python-数据类型-字符串(Str)

"""
1、str.capitalize() : 首字母大写,其余小写
2、str.casefold(): 消除大小写
3、str.center():  居中字符串,左右空格或填入字符串,与str.ijust()左对齐功能相似,
4、str.count(): 指定值在字符串中出现的次数
5、str.encode(): 对字符串进行UTF-8编码
6、str.endwith(): 检查字符串是否以''结尾
7、str.expandtabs(): 设置字符串字符之间的尺寸
8、str.find(): 查找指定值的首次出现位置,没找到返回-1
9、str.format(): 格式化指定的值,并将其插入字符串的占位符内
10、str.index(): 与find功能类似,查找指定值首次出现的位置,不同的是找不到子类会报错
11、str.isalnum(): 检查文本中所有的字符是否都是字母数字
12、str.istitle(): 检查每个字符是否都以大写字母开头
13、str.join(): 获取可迭代对象中的所有项目,如元组或列表,将连接到一个字符串中
14、str.partition(): 搜索指字的字符串,并将该字符串拆分成三部分:a、指定字符串之前的部分;b、指定字符串;c、指字字符串之后的部分
15、str.split(): 在指定的分隔符处拆分字符串,并返回列表
16、str.title(): 把每个单词的首字母转换成大写
17、str.zfill(): 在字符串的开头添加零,直到到达指定的长度
"""

#1、str.capitalize() : 首字母大写,其余小写 a = "Hello World" print(a.capitalize()) #执行结果 :Hello world #2、str.casefold(): 消除大小写 a = "Hello World" print(a.casefold()) #执行结果 :hello world #3、str.center(): 居中字符串,左右空格或填入字符串 a = "Hello World" print(a.center(40,"*")) #执行结果 :**************Hello World*************** print(a.center(40)) #执行结果 : Hello World #4、指定值在字符串中出现的次数 a = "Hello World" print(a.count('o')) #执行结果 :2 #5、str.encode(): 对字符串进行UTF-8编码 b = "My name is Ståle" print(b.encode()) #errors可选参数,规定错误方法,合法值是backslashreplace、ignore、nameplace,replace等 print(b.encode(encoding="ascii",errors="ignore")) #6、str.endwith(): 检查字符串是否以''结尾 txt = "Hello, welcome to my world." print(txt.endswith('.')) print(b.endswith('Ståle')) # str.startwith(): 检查字符串是否以''开头 txt = "Hello,welcome to my world" print(txt.startswith('Hello')) #7、str.expandtabs(): 设置字符串字符之间的尺寸 txt = "H e l l o" print(txt) #执行结果 :H e l l o print(txt.expandtabs(2)) #执行结果 : H e l l o #8、str.find(): 查找指定值的首次出现位置,没找到返回-1 txt = "Hello, welcome to my world." print(txt.find("vv")) #执行结果:返回-1,找不到该值 print(txt.find("o")) #执行结果:返回4 ,首次出现在第4位,从0开始计算 print(txt.find('e',5,10)) #执行结果 :返回8,只搜索位置5到10的值 #str.rfind(): 查找指定值最后一次出现的位置,没找到返回-1 print(txt.rfind("o")) #9、str.format(): 格式化指定的值,并将其插入字符串的占位符内 txt = "Welcome to my world,{}" print(txt.format("vv")) #执行结果: Welcome to my world,vv txt1 = "My name is {fname},I'm {age}".format(fname='vv',age=18) print(txt1) #执行结果: My name is vv,I'm 18 txt2 = "My name is {},I'm {}".format('vv',18) print(txt2) #执行结果: My name is vv,I'm 18 #11、str.isalnum(): 检查文本中所有的字符是否都是字母数字,非字母数字的例子:(space)!#%&? txt = "company1 " print(txt.isalnum()) #执行结果:False txt1 = "company18" print(txt1.isalnum()) #执行结果: True #检查所有字符是否都在字母表里 txt = "CompanyX*" print(txt.isalpha()) #检查所有字符是否都是数字 txt = "5432532" print(txt.isdigit()) #检查所有字符是否是标识符,如字母数字字母组合,如以数字开头则返回False txt = "vv_1068" print(txt.isidentifier()) #执行结果: True txt = "1068_vv" print(txt.isidentifier()) #执行结果: False #检查每个字符是否都以大写字母开头 txt = "Hello, And Welcome To My World!" print(txt.istitle()) #13、str.join(): 获取可迭代对象中的所有项目,如元组或列表,将连接到一个字符串中 txt = ["hello","world","vv"] print('_'.join(txt)) #执行结果: hello_world_vv txt1 = ("Bill","Steve","Elon") print('&'.join(txt1)) #执行结果: Bill&Steve&Elon myDic = {"name":"vv","age":18} mysixual = "Female" print(mysixual.join(myDic)) #执行结果: nameFemaleage (在使用字典作为迭代器时,只join字典的健,不join值) #14、str.partition(): 搜索指字的字符串,并将该字符串拆分成三部分:a、指定字符串之前的部分;b、指定字符串;c、指字字符串之后的部分 txt = "I could eat bananas all day" print(txt.partition("bananas")) #执行结果: ('I could eat ', 'bananas', ' all day') #str.rpartition(): 搜索指定字符串最后一次出现的位置,并交其拆分成三部分:字符之前部分,字符,字符之后部分 txt = "I could eat bananas all day, bananas are my favorite fruit" print(txt.rpartition("bananas")) #执行结果: ('I could eat bananas all day, ', 'bananas', ' are my favorite fruit') #str.replace():替代 txt = "I could eat bananas all day" print(txt.replace("bananas","apples")) #执行结果: I could eat apples all day txt = "one one was a race horse,two two was one too" print(txt.replace("one","three",2)) #执行结果:three three was a race horse,two two was one too,只替换前两个 #15、str.split(): 在指定的分隔符处拆分字符串,并返回列表 txt = "apple,banana,cherry" print(txt.split(',')) #执行结果:['apple', 'banana', 'cherry'] ## 将 max 参数设置为 1,将返回包含 2 个元素的列表 print(txt.rsplit(',',1)) #执行结果: ['apple,banana', 'cherry'] #将字符串拆分成列表,拆份在换行符处完成 txt = "Thank you for your visiting Welcome to China." print(txt.splitlines()) #执行结果: ['Thank you for your visiting ', ' Welcome to China.'] #16、str.title(): 把每个单词的首字母转换成大写 txt = "Welcome to my world" print(txt.title()) #执行结果: Welcome To My World #17、str.zfill(): 在字符串的开头添加零,直到到达指定的长度 txt = "50" print(txt.zfill(20)) #执行结果: 00000000000000000050
三十六般武艺,七十二般变化,修练出个人品牌并发出光芒
原文地址:https://www.cnblogs.com/deeptester-vv/p/14889595.html