1、len()
计算字符串长度。
a = "i like you"
print(len(a))
打印输出结果:10
2、capitalize()
首字母大写。
a = "i like you"
print(a.capitalize())
打印输出结果:
I like you
3、count()
统计字符串中某个字符的个数。
a = "i like you"
print(a.count("i"))
打印输出结果:2
4、center()
将字符串放中间,两边填充(默认为空格)。
a = "i like you"
b = a.center(50)
print(b)
b = a.center(50,"*")
print(b)
打印输出结果:
i like you
********************i like you********************
5、endswith("xx")
判断字符串是否以"xx"结尾。
a = "i like you"
print(a.endswith("you"))
print(a.endswith("yo"))
打印输出结果:
True
False
6、expandtabs()
把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8。
a = "i \tlike you"
print(a)
print(a.expandtabs())
print(a.expandtabs(tabsize=16))
打印输出结果:
i like you
i like you
i like you
st = "this is\tstring example....wow!!!";
print(st)
print(st.expandtabs(8))
print(st.expandtabs(16))
打印输出结果:
this is string example....wow!!!
this is string example....wow!!!
this is string example....wow!!!
st = "this is\tstring example....wow!!!";
print("123"+st)
print("123"+st.expandtabs(8))
print("123"+st.expandtabs(16))
打印输出结果:
123this is string example....wow!!!
123this is string example....wow!!!
123this is string example....wow!!!
7、find('xx')
找到''xx"相对字符串首字母的偏移位置。
a = "i like you"
offset = a.find("like")
print(offset)
print(a[offset:])
print(a[:offset])
打印输出结果:
2
like you
i
8、format()
格式化字符串。
a = "my name is {name} and i am {year} old"
b = a.format(name="jack",year=20)
print(a)
print(b)
打印输出结果:
my name is {name} and i am {year} old
my name is jack and i am 20 old
9、 format_map()
格式化字符串。
a = "my name is {name} and i am {year} old"
b = a.format_map({'name':'jack', 'year':25})
print(b)
打印输出结果:
my name is jack and i am 25 old
10、 isalnum()
字符串是否是字母与数字。
如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False。
a = "abc123"
b = "***abc123"
c =" "
print(a.isalnum())
print(b.isalnum())
print(c.isalnum())
打印输出结果:
True
False
False
11、isalpha()
判断字符串是否之由字母组成。
a = "hello"
b = "abc123"
c = "***abc123"
print(a.isalpha())
print(b.isalpha())
print(c.isalpha())
打印输出结果:
True
False
False
12、isdigit()
是否是数字。
a = "123"
b = "hello"
print(a.isdigit())
print(b.isdigit())
打印输出结果:
True
False
13、isdecimal()
检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
a = "this2009"
b = "2018"
c = "你好"
d = "你好".encode("utf-8")
e = str(123)
print(a.isdecimal())
print(b.isdecimal())
print(c.isdecimal())
print(e.isdecimal())
print(d.isdecimal())
打印输出结果:
False
True
False
True
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-f99a056296e2> in <module>()
8 print(c.isdecimal())
9 print(e.isdecimal())
---> 10 print(d.isdecimal())
AttributeError: 'bytes' object has no attribute 'isdecimal'
14、isidentifier()
判断是不是一个合法的标识符。
标识符必须以字母(大小写均可)或者"_"开头,接下来可以重复0到多次(字母|数字|"_")。
class API(object):
def get(self):
print("get")
def post(self):
print("post")
api = API()
attr_list = ["name", "123", "!!!","__set__","_delete","test_123"]
for attr in attr_list:
if attr.isidentifier():
setattr(api, attr, True)
print(api.__dict__)
打印输出结果:
{'name': True, '__set__': True, '_delete': True, 'test_123': True}
15、isnumeric()
检测字符串是否只由数字组成。
这种方法是只针对unicode对象。定义一个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可。
a = "hello2019"
b = "123456"
c = "0x12A"
d = "33A"
print(a.isnumeric())
print(b.isnumeric())
print(c.isnumeric())
print(d.isnumeric())
打印输出结果:
False
True
False
False
16、istitle()
检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
a = "i like you"
b = "I Iike You!!"
print(a.istitle())
print(b.istitle())
打印输出结果:
False
True
17、isupper()
检测字符串中所有的字母是否都为大写。
a = "HELLO"
b = "Ok"
print(a.isupper())
print(b.isupper())
打印输出结果:
True
False
18、join()
将序列中的元素以指定的字符连接生成一个新的字符串。
word_list =["I", 'like', 'you', '!','!']
title1 = " ".join(word_list)
title2 = "_".join(word_list)
print(title1)
print(title2)
打印输出结果:
I like you ! !
I_like_you_!_!
19、ljust()
返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
title = "My name is Jack,I'm 25 years old!"
new_title_1 = title.ljust(40,"*")
new_title_2 = title.ljust(20,"*")
print(title)
print(new_title_1)
print(new_title_2)
打印输出结果:
My name is Jack,I'm 25 years old!
My name is Jack,I'm 25 years old!*******
My name is Jack,I'm 25 years old!
20、rjust()
返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。
title = "My name is Jack,I'm 25 years old!"
new_title_1 = title.rjust(40,"*")
new_title_2 = title.rjust(20,"*")
print(title)
print(new_title_1)
print(new_title_2)
打印输出结果:
My name is Jack,I'm 25 years old!
*******My name is Jack,I'm 25 years old!
My name is Jack,I'm 25 years old!
21、lower()
将字符串转换成小写。
a = "HELLO"
b = "Ok"
print(a.lower())
print(b.lower())
打印输出结果:
hello
ok
22、upper()
将字符串转换成大写。
a ="Hello"
b = "ok"
print(a.upper())
print(b.upper())
打印输出结果:
HELLO
OK
23、lstrip()
用于截掉字符串左边的空格或指定字符。
res = url.lstrip("https")
ret = res.lstrip()
print(res)
print(ret)
打印输出结果:
print( '\nAlex'.lstrip() )
打印输出结果:
Alex
24、rstrip()
用于截掉字符串右边的空格或指定字符。
print(url.rstrip("b=2"))
打印输出结果:
print( 'Alex\n'.rstrip() )
打印输出结果:
Alex
25、strip()
用于移除字符串头尾指定的字符(默认为空格)或字符序列。
该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
print( ' Alex\n'.strip() )
打印输出结果:
Alex
26、maketrans()
创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。两个字符串的长度必须相同,为一一对应的关系。
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)
title = "this is string example....wow!!!"
print(title.translate(trantab))
打印输出结果:
th3s 3s str3ng 2x1mpl2....w4w!!!
27、replace()
把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
a = "I like you,do you like me?"
b = a.replace('like', 'love', 1)
c = a.replace('like', 'love', 2)
print(b)
print(c)
打印输出结果:
I love you,do you like me?
I love you,do you love me?
28、rfind()
返回字符串最后一次出现的位置,如果没有匹配项则返回-1。
a = "I like you,do you like me?"
print(a.rfind('me'))
打印输出结果:23
29、split()
通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 次。
print('1+2+3+4'.split('+'))
print('1+2+3+4+5'.split('+',2))
print('1+2+3+4+5'.split('+',3))
打印输出结果:
['1', '2', '3', '4']
['1', '2', '3+4+5']
['1', '2', '3', '4+5']
30、splitlines(keepends)
按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
print('1+2\n+3+4'.splitlines())
print('1+2\n+3+4'.splitlines(True))
print('1+\r\n2\n+3+\r4'.splitlines())
打印输出结果:
['1+2', '+3+4']
['1+2\n', '+3+4']
['1+', '2', '+3+', '4']
31、swapcase()
用于对字符串的大小写字母进行转换。
a = "I like you!"
b = a.swapcase()
print(a)
print(b)
打印输出结果:
I like you!
i LIKE YOU!
32、title()
返回"标题化"的字符串,所有单词的首个字母转化为大写,其余字母均为小写。
a = "I like you!"
b = a.title()
print(b)
打印输出结果:
I Like You!
33、 zfill()
指定长度的字符串,原字符串右对齐,前面填充0。
money = "2050"
money = money.zfill(8)
print(money)
打印输出结果:
00002050