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

python---字符串

作者头像
py3study
发布2020-01-14 15:35:32
3960
发布2020-01-14 15:35:32
举报
文章被收录于专栏:python3

特性:不可修改 

代码语言:javascript
复制
test = "my name is fengxiaoli"

###确定字符串中字符位置
print(test.rfind("m"))                  #找到m所在的位置,以最右边的为准
print(test.find("name"))                #找到字符串中name的开头位置,这里是3
print(test[0:test.find("name")])        #字符串切片,从0到name的开头位置3


###字符串补齐
print(test.center(50,"-"))              #给定字符串长度50,不足50添加-补齐
print(test.ljust(50,"*"))               #给定字符串长度50,不足50添加*补齐
print(test.rjust(50,"-"))               #给定字符串长度50,不足50添加*补齐

        
#字符串判断
print("12ADFAagdadf".isalnum())          #判断是否是英文字母和数字,是返回Ture
print("aAf".isalpha())                   #判断是否是英文字符串
print("34".isdigit())                    #判断是否是整数
print("adfadf".isidentifier())           #判断是否是一个合法的标识符(变量)
print(" ".isspace())                     #判断是否是一个空格
print(" My Name Is".istitle())           #判断是否是一个标题
print("ni hao".isupper())                #判断字符串是否是大写
print(test.endswith("li"))               #判断字符串是否由li结尾,是返回Ture
print(test.startswith("my"))             #


#字符串转化
print("ASDF".lower())                         #将大写转化为小写
print("sdafa".upper())                        #将小写转化为大写
print("ASdfadADFAad".swapcase())              #大小写互换
print(" my name is".title())                  #将其转化为title
print(test.expandtabs(tabsize=30))            #将字符串中tab键(\t)转化为30个空格
print(" my name is ".replace("m","M",1))      #将m替换成大写的M,只替换1个
#输出:My name is


#去开头结尾空格和换行符
print("    \nmy name is is".strip())           #去掉开头结尾的空格和换行符
print("fnegxiaoli\n".rstrip())                 #去掉右边的空格和换行符
print("   \nfnegxiaoli\n".lstrip())            #去掉左边的空格和换行符


#字符串分割 
print("adf+na +df".split("+"))                 #指定分隔符分割字符串
print("add\nadf\nadfg".splitlines())           #按换行符分割


#将列表转化为字符串
print("+".join(["1","2","3"]))                 #将后面的列表按+号分隔,转化为字符串


#补充:
p = str.maketrans("abcdef","123456")            #将abcdef与123456对应
print("fengxiaoli".translate(p))                #然后fengxiaoli中有属于abcdef字母的用与之对应的数字替换
#输出:65ngxi1oli

test1 = "my name is {_name} ,age is {_age}"
print(test1.format(_name="fengxiaoli",_age=24))     #格式化输出

print(test.capitalize())                        #将字符串首字母大写
print(test.count("m"))                          #统计字符串中m的个数
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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