前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python学习之字符串转换

python学习之字符串转换

作者头像
py3study
发布2020-01-17 11:37:14
3860
发布2020-01-17 11:37:14
举报
文章被收录于专栏:python3python3

配置环境:python 3.6   python编辑器:pycharm

代码如下:

代码语言:javascript
复制
#!/usr/bin/env python
#-*- coding: utf-8 -*-

def strCase():
    "字符串大小写转换"
    print("演示字符串大小写转换")
    print("演示字符串S赋值为:'  ThIs is a PYTHON  '")
    S = '  ThIs is a PYTHON  '
    print("大写转换成小写:\tS.lower() \t= %s"%(S.lower()))
    print("小写转换成大写:\tS.upper() \t= %s"%(S.upper()))
    print("大小写转换:\t\tS.swapcase() \t= %s"%(S.swapcase()))
    print("首字母大写:\t\tS.title() \t= %s"%(S.title()))
    print('\n')


def strFind():
    "字符串搜索、替换"
    print("演示字符串搜索、替换等")
    print("演示字符串S赋值为:'  ThIs is a PYTHON  '")
    S = '  ThIs is a PYTHON  '
    print("字符串搜索:\t\tS.find('is') \t= %s"%(S.find('is')))
    print("字符串统计:\t\tS.count('s') \t= %s"%(S.count('s')))
    print("字符串替换:\t\tS.replace('Is','is') = %s"%(S.replace('Is','is')))
    print("去左右空格:\t\tS.strip() \t=#%s#"%(S.strip()))
    print("去左边空格:\t\tS.lstrip() \t=#%s#"%(S.lstrip()))
    print("去右边空格:\t\tS.rstrip() \t=#%s#"%(S.rstrip()))
    print('\n')


def strSplit():
    "字符串分割、组合"
    print("演示字符串分割、组合")
    print("演示字符串S赋值为:'  ThIs is a PYTHON  '")
    S = '  ThIs is a PYTHON  '
    print("字符串分割:\t\tS.split() \t= %s"%(S.split()))
    print("字符串组合1: '#'.join(['this','is','a','python']) \t= %s"%('#'.join(['this','is','a','python'])))
    print("字符串组合2: '$'.join(['this','is','a','python']) \t= %s"%('$'.join(['this','is','a','python'])))
    print("字符串组合3: ' '.join(['this','is','a','python']) \t= %s"%(' '.join(['this','is','a','python'])))
    print('\n')

def strTest():
    "字符串测试"
    print("演示字符串测试")
    print("演示字符串S1赋值为:'abcd'")
    S1 = 'abcd'
    print("测试S.isalpha() = %s"%(S1.isalpha()))
    print("测试S.isdigit() = %s"%(S1.isdigit()))
    print("测试S.isspace() = %s"%(S1.isspace()))
    print("测试S.islower() = %s"%(S1.islower()))
    print("测试S.isupper() = %s"%(S1.isupper()))
    print("测试S.istitle() = %s"%(S1.istitle()))

if __name__ == '__main__':
    strCase()
    strFind()
    strSplit()
    strTest()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档