首页 > 代码库 > python 字符串的方法
python 字符串的方法
1、首字母大写
name = "wuyuchao"
result = name.capitalize()
print(result)
返回 Wuyuchao
------------------------------------------------------------------------------------------------------------
2、内容居中
name = "wuyuchao"
result = name.center(20,‘*‘) #20代表长度,*代表填充字符
print(result)
返回 ******wuyuchao******
------------------------------------------------------------------------------------------------------------
3、子序列个数
name = "wuyuchao"
result = name.count(‘yu‘)
print(result)
返回 1 yu出现了1次
result2 = name.count(‘nnn‘)
print(result2)
返回 0 nnn没有出现
------------------------------------------------------------------------------------------------------------
4、编码
name = "吴玉超"
result = name.encode("gbk")
print(result)
------------------------------------------------------------------------------------------------------------
5、大小写转换
name = "wuyuchao"
result = name.swapcase()
print(result)
返回 WUYUCHAO
------------------------------------------------------------------------------------------------------------
6、是否以***结束
name = "wuyuchao"
result1 = name.endswith("o")
result2 = name.endswith("x")
print(result1,result2)
返回 True False
------------------------------------------------------------------------------------------------------------
7、是否以***开始
name.startswith()
------------------------------------------------------------------------------------------------------------
8、子序列出现的次数
name = "wwuuyyuucchhaaoo"
result = name.count(‘u‘)
print(result)
返回 4,字符串中出现了4次u
------------------------------------------------------------------------------------------------------------
9、将tab替换为8个空格
name = "w\tuyuchao"
print(name) 返回 w uyuchao
result = name.expandtabs()
print(result) 返回 w uyuchao
------------------------------------------------------------------------------------------------------------
10、查询子序列在字符串中的位置,如果没有,返回-1
name = "wuyuchao"
result = name.find("w")
result2 = name.find("h")
result3 = name.find("x")
print(result,result2,result3)
返回 w 0 ,h 5 ,x -1 字符串中不包含x 返回-1
------------------------------------------------------------------------------------------------------------
11、查询子序列在字符串中的位置,如果没有,直接报错
name = "wuyuchao"
result = name.index("w")
result2 = name.index("h")
返回 w 0 ,h 5
result3 = name.index("x")
直接报错
------------------------------------------------------------------------------------------------------------
12、字符串格式化
name = "wu{0}{1}" #数字从0开始
result = name.format("yu","chao")
print(result)
返回 wuyuchao
name = "wu{ming}{zi}"
result = name.format(ming="yu",zi="chao")
print(result)
返回 wuyuchao
------------------------------------------------------------------------------------------------------------
13、是否是字母和数字
name = "wuyuchao123"
print(name.isalnum())
返回True
------------------------------------------------------------------------------------------------------------
14、是否是字母
name = "FABacasd"
print(name.isalpha()) 返回True
name2 = "FABacasd1"
print(name2.isalpha()) 返回False
------------------------------------------------------------------------------------------------------------
15、是否是数字
name = "1"
print(name.isdigit())
True
name = "a"
print(name.isdigit())
False
------------------------------------------------------------------------------------------------------------
16、是否为小写
name.islower()
------------------------------------------------------------------------------------------------------------
17、是否为大写
name.isupper()
------------------------------------------------------------------------------------------------------------
18、字符串连接
li = [‘w‘,‘u‘,‘y‘,‘u‘,‘c‘,‘h‘,‘a‘,‘o‘]
result = "".join(li)
print(result)
返回 wuyuchao
------------------------------------------------------------------------------------------------------------
19、内容右对齐
name = "wuyuchao"
result = name.rjust(20)
print(result)
返回 wuyuchao
------------------------------------------------------------------------------------------------------------
20、转换为小写
name = "WUYUCHAO"
result = name.swapcase()
print(result)
返回 wuyuchao
------------------------------------------------------------------------------------------------------------
21、移除右侧空白
name = "wuyuchao "
print(name)
result = name.rstrip()
print(result)
name 右侧有空格
result 右侧没有了空格
------------------------------------------------------------------------------------------------------------
22、分割,前、中、后三部分
name = "wuyuchao"
result = name.partition("yu")
result2 = name.partition("wu")
print(result,result2)
返回 (‘wu‘, ‘yu‘, ‘chao‘) (‘‘, ‘wu‘, ‘yuchao‘)
------------------------------------------------------------------------------------------------------------
23、替换
name = "wuyuchao"
result = name.replace("yu","YU")
print(result)
返回 wuYUchao
------------------------------------------------------------------------------------------------------------
python 字符串的方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。