Python学习---string功能展示

#字符串拼接
a = "abc"
b = "123"
print (a+b)#abc123
c = ''.join((a,b))
print(c)#abc123
#总结: “”.join()相较于“+”可以避免内存空间的浪费,并且更加快速


##########字符串内置功能##################
str = "hello word!!!"
t = str.count("l")#统计元素个数
w = str.capitalize()# Hello word!!!首字母大写
q = str.center(20,"-")#---hello word!!!---- #占用20个字符,中间文字部分居中,其他的使用-替代
print(str.endswith("!!!"))#判断以什么字符结尾,返回值为bool型
print(str.startswith("h"))#判断是否有“h”开头,用来处理文件
str1 = 'my python\tbook'
print(str1.expandtabs(tabsize=30))#指定字符串中“\t”的空格位数
print(str.find("o"))#查找到第一个元素,并返回索引值
str2 = "my name is {name} is {age}"
#print(str2.format(name = "duyang"))#格式化输出
print(str2.format_map({"name": "duyang", "age":26}))
#总结:format与format_map的区别在于,后者使用词典的个式输入

print("abc杜洋#".isalnum())#其中汉字满足其定义,而#返回false
print("abc123".isalnum())#判断字符串中是否为字母和数字,返回bool型
print("ac".isdecimal())#判断是否为10进制的数
print("123".isdigit())#判别是否为整形 数字
print("23.333".isdecimal())#同上
print("34abd".isidentifier())#检查是否为非法字符
print("abc".islower())#判断是否为纯小写
print("是否为纯大写:%s"%"ABC".isupper())#是否为纯大写
print(" ".isspace())#判断是否为空格
print("My Title".istitle())#是否为标题:每个单词首字母大写
print("My Title".lower())#大写变小写
print("My Title".upper())#小写变大写
print("My Title".swapcase())#大小写相互转换
print("My Title".ljust(50,"#"))#My Title##########################################
print("My Title".rjust(50,"#"))##########################################My Title
print("My    Title".strip())#stip用来清除换行符合空格
print("ok")
print("My    Title".rstrip())#stip用来清除换行符合空格
print("My    Title".lstrip())#stip用来清除换行符合空格
print('My title title'.replace('title','book',1))#replace 表示替换,后面的1表示替换次数
print("My title book!!".split(" "))#以空格分格,返回三个值,以列表返回#['My', 'title', 'book!!']
print("My title book!!".split("o",1))#只分割一次,以左侧为准

print("My title book!!".title())

#############################重要的字符串方法###########################


q = str.center(20,"-")#---hello word!!!----
print(str.startswith("h"))#判断是否有“h”开头,用来处理文件
print(str.find("o"))#查找到第一个元素,并返回索引值


str2 = "my name is {name} is {age}"
#print(str2.format(name = "duyang"))#格式化输出
print(str2.format_map({"name": "duyang", "age":26}))
#总结:format与format_map的区别在于,后者使用词典的个式输入
print("123".isdigit())#判别是否为整形 数字

print("My Title".lower())#大写变小写
print("My Title".upper())#小写变大写


print("My title book!!".split(" "))#以空格分格,返回三个值,以列表返回
#['My', 'title', 'book!!']
print("My title book!!".split("o",1))#只分割一次,以左侧为准


print('My title title'.replace('title','book',1))
#replace 表示替换,后面的1表示替换次数

猜你喜欢

转载自blog.csdn.net/qq_33661910/article/details/80985179