Python字符串处理函数总结
本文为Python字符串处理使用方法,直接 Ctrl + f 搜索关键字即可,方便自己使用。
bit_length() #计算二进制字符串
sum = 10
v=sum.bit_length()
print(v)
输出 4
upper() #小写转大写
aa = guoqingkuaile
upper(aa)
输出: GUOQINGKUAILE
lower() #大写转换成小写
test = "WELCOME"
v = test.lower()
print(v)
输出: welcome
capitalize() #将字符串的第一位改成大写
test = "welcome"
v = test.capitalize()
print(v)
输出: Welcome
center(20,"*") # 20表示总长度,*代表空格用什么填充
test = Welcome
v = test.center(20,"*")
print(v)
输出: ******Welcome*******
endswith('x') #如果匹配字符串以x结尾,那么返回True,否则flase
test = "welcome"
v = test.endswith('x')
print(v)
输出: False
test = "welcomex"
v = test.endswith('x')
print(v)
输出: True
startswith #如果匹配字符串以x开头,那么返回True,否则flase
test = "welcome"
v = test.endswith('x')
print(v)
输出: False
test = "xwelcome"
v = test.endswith('x')
print(v)
输出: True
find() #如果找到此方法返回的索引,否则返回-1。
test = "welcome"
v = test.find('el',1,5)
print(v)
输出: 1
test = "welcome"
v = test.find('a',1,5)
print(v)
输出: -1
format() #格式化,将一个字符串中的占位符替换为指定的值
test = 'i am {name}, age {n}'
v = test.format(name='yunwei',n=21)
print(v)
输出: i am yunwei, age 21
#根据出现的顺序替换
test = 'i am {0}, age {1}'
v = test.format('yunwei',21)
print(v)
输出: i am yunwei, age 21
isalnum() #判断字符串中只包含数字和字母
test = "This year is 2019"
v = isalnum(test)
print(v)
输出: False
test = "goodbuy2018"
v = isalnum(test)
print(v)
输出: True
expandtabs(20) #20表示字符串长度为20,当匹配到\t时候,不足20用空白字符填充。
test = "username\tpassword"
v = test.expandtabs(20)
print(v)
输出: username password
isalpha() #如果字符串所有都为字母则返回Ture
test = "welcome"
v = test.isalpha()
print(v)
输出: True
test = "welcome!!"
v = test.isalpha()
print(v)
输出: flase
isspace() #如果字符串所有都是空格,那么则返回True
test = " "
v = test.isalpha()
print(v)
输出: True
istitle() #判断首字母是否是大写,是则返回True
test = "Welcome"
v = test.istitle()
print(v)
输出: True
islower() #判断字符串是否为小写
test = "welcome"
v = test.islower()
print(v)
输出: True
test = "Welcome"
v = test.islower()
print(v)
输出: Flase
lower() #将字符串变为小写字符
test = "WELCOME"
v = test.lower()
print(v)
输出: welcome
isupper() #判断字符串是否为大写
test = "WELCOME"
v = test.isupper()
print(v)
输出: True
test = "Welcome"
v = test.isupper()
print(v)
输出: Flase
upper() #将字符串变为大写字符
test = "Welcome"
v = test.upper()
print(v)
输出: WELCOME
title() #将字符串转换为标题,其首字母为大写
test = "welcome come to guangzhou"
v = test.istitle()
print(v)
输出: Welcome Come To Guangzhou
join() #每一个元素按照分隔符进行拼接
test = "国庆快乐"
v = "_".join(test)
print(v)
输出: 国_庆_快_乐
ljust(20,"*") #向右填充*字符,总长度为20
test = "lalalalala"
v = ljust(20,"*")
print(v)
输出: lalalalala**********
rjust(20,"*") #向做填充*字符,总长度为20
test = "lalalalala"
v = ljust(20,"*")
print(v)
输出: **********lalalalala
清除字符
rstrip() #默认清除右边空白字符
lstrip() #默认清除左边空白字符
strip() #默认清除两边空白字符
maketrans() #对应字符替换
test = "abcdefg"
m = str.maketrans("acf","123")
c = test.translate(m)
print(c)
输出为 1b2de3
rpartition("co") #从目标字符串的末尾也就是右边开始搜索分割符,分割符会显示。
test = "comecomecome"
v = test.rpartition("co")
print(v)
输出: 'comecome', 'co', 'me'
split('co',1) #以co为分隔符分割正序,不包含分割的元素,1表示以第一个co为分割符
test = "comecomecome"
v = test.split("co", 1)
print(v)
输出: '', 'mecomecome'
replace("18","21",1) #字符串替换,1表示替换第一个18.
test = "i am 18 years"
v = test.replace("18","21",1)
print(v)
输出: i am 21 years
range(0,100,5) #创建连续的数字,0-99 ,5表示等差
test = range(0,100,5)
for n in test:
print(n)
输出:
0
5
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
#pyton 3.x版本只有用for循环才会存入内存,