1.字符串的转化(str()):
1 n = 190
2 s1 = str(n)
3 print(type(s1),s1)
结果:
<class 'str'> 190
2.首字母大写(capitalize())
1 n = "ailadi"
2 s1 = n.capitalize()
3 print(s1)
4 结果:
5 Ailadi
3.字符串全变小写(casefold())
1 n = "aiLadi"
2 s1 = n.casefold()
3 print(s1)
4 结果:
5 ailadi
4.输出字符串的长度及填充
a. center( 长度,填充字符 ):将字符放在中间
1 n = "ailadi"
2 s1 = n.center(20,'#')
3 print(s1)
4 结果:
5 #######ailadi#######
b. rjust( 长度,填充字符 ):将字符放在右边
1 n = "ailadi"
2 s1 = n.rjust(20,'#')
3 print(s1)
4 结果:
5 ##############ailadi
c. ljust( 长度,填充字符 ):将字符放在左边
1 n = "ailadi"
2 s1 = n.ljust(20,'#')
3 print(s1)
4 结果:
5 ailadi##############
5.计算字符出现次数(count(sub,__start,__end))
1 n = "ailadi"
2 s1 = n.count('i',1,4)
3 print(s1)
4 结果:
5 1
1 n = "ailadi"
2 s1 = n.count('il',1,4)
3 print(s1)
4 结果:
5 1
6.对比字符串最后结尾返回bool值(endswith(sub,__start,__end))
n = "ailadi"
s1 = n.endswith('i')
print(s1)
结果:
True
n = "ailadi"
s1 = n.endswith('s')
print(s1)
结果:
False
n = "ailadi"
s1 = n.endswith('l',0,3)
print(s1)
结果:
True
7.对比字符串开始字符返回bool值(startswith(sub,__start,__end))
8.expandtabs用法(expandtabs(n))
1 n = "aaaaaaaaaaaaa aaaaaaaaaaaaa"
2 s1 = n.expandtabs(10)
3 print(s1)
4 结果:
5 aaaaaaaaaaaaa aaaaaaaaaaaaa
6
7 n = "ail adi
ail adi
ail adi
ail adi
ail adi
"
8 s1 = n.expandtabs(10)
9 print(s1)
10 结果:
11 ail adi
12 ail adi
13 ail adi
14 ail adi
15 ail adi
9.寻找子序列,找到第一个时输出位置(find(sub,__start.__end))
1 n = "ailadi"
2 s1 = n.find("la")
3 print(s1)
4 结果:
5 2
6
7 n = "ailadi"
8 s1 = n.find("li")
9 print(s1)
10 结果:
11 -1
12
13 n = "ailadi"
14 s1 = n.find("l",1,4)
15 print(s1)
16 结果:
17 2
10.格式化替换变量(format(),format_map())
1 n = "i am {name},age = {age}"
2 s1 = n.format(name = "ailadi",age = 19)
3 print(s1)
4 结果:
5 i am ailadi,age = 19
1 n = "i am {0},age = {1}"
2 s1 = n.format("ailadi",19)
3 print(s1)
4 结果:
5 i am ailadi,age = 1
1 n = "i am {name},age = {age}"
2 s1 = n.format_map({'name':"ailadi",'age':19})
3 print(s1)
4 结果:
5 i am ailadi,age = 19
11.判断字符串里面全是字母和数字返回true否则返回false(isalnum())
1 n = "ailadi"
2 s1 = n.isalnum()
3 print(s1)
4 结果:
5 True
6
7
8 n = "ailadi111"
9 s1 = n.isalnum()
10 print(s1)
11 结果:
12 True
13
14 n = "ailadi+111"
15 s1 = n.isalnum()
16 print(s1)
17 结果:
18 False
12.判断字符串里面全是字母返回true否则返回false(isalpha())
1 n = "ailadi111"
2 s1 = n.isalpha()
3 print(s1)
4 结果:
5 False
6
7 n = "ailadi"
8 s1 = n.isalpha()
9 print(s1)
10 结果:
11 True
13.判断字符串里面全是数字返回true否则返回false(isdecimal() 、isdigit()、isnumeric())
1 n = "129834981"
2 s1 = n.isdecimal()
3 s2 = n.isdigit()
4 s3 = n.isnumeric()
5 print(s1,s2,s3)
6 结果:
7 True True True
8
9 n = "②"
10 s1 = n.isdecimal()
11 s2 = n.isdigit()
12 s3 = n.isnumeric()
13 print(s1,s2,s3)
14 结果:
15 False True True
16
17 n = "二"
18 s1 = n.isdecimal()
19 s2 = n.isdigit()
20 s3 = n.isnumeric()
21 print(s1,s2,s3)
22 结果:
23 False False True
14.查看字符串是否时字母或者下划线开头(isidentifier())
n = "sad"
s1 = n.isidentifier()
print(s1)
结果:
True
n = "123213"
s1 = n.isidentifier()
print(s1)
结果:
False
n = "_123213"
s1 = n.isidentifier()
print(s1)
结果:
True
15.查看和转化字符串的大小写
------查看字符串中是否全是小写
1 n = "aaDsa"
2 s1 = n.islower()
3 print(s1)
4 结果:
5 False
6
7 n = "aadsa"
8 s1 = n.islower()
9 print(s1)
10 结果:
11 True
12
13 n = "aad哎222sa"
14 s1 = n.islower()
15 print(s1)
16 结果:
17 True
------将字符串变为小写
1 n = "AiLaDi"
2 s1 = n.lower()
3 print(s1)
4 结果:
5 ailadi
------查看字符是否全为大写
1 n = "AILADI"
2 s1 = n.isupper()
3 print(s1)
4 结果:
5 Ture
------将字符串全部转换为大写
1 n = "ailadi"
2 s1 = n.upper()
3 print(s1)
4 结果:
5 AILADI
16.查看字符串是否存在不可直接打印的字符例如: 、
(isprintable())
1 n = "asdasd"
2 s1 = n.isprintable()
3 print(s1)
4 结果:
5 True
6
7 n = "asd asd"
8 s1 = n.isprintable()
9 print(s1)
10 结果:
11 False
17.查看字符串中是否全是空格(isspace())
1 n = "asdasd"
2 s1 = n.isspace()
3 print(s1)
4 结果:
5 False
6
7 n = "asd asd"
8 s1 = n.isspace()
9 print(s1)
10 结果:
11 False
12
13 n = " "
14 s1 = n.isspace()
15 print(s1)
16 结果:
17 True
18.标题的转换和判断转换title()、判断istitle()
1 n = "do not use on a shared computer"
2 s1 = n.istitle()
3 print(s1)
4 s2 = n.title()
5 print(s2)
6 s3 = s2.istitle()
7 print(s3)
8 结果:
9 False
10 Do Not Use On A Shared Computer
11 True
*****19.将字符串中的每一个元素用规定的字符串拼接起来(join())
1 n = "aaaaaaaaa"
2 s = " _ @ s 1 "
3 s1 = s.join(n)
4 print(s1)
5 结果:
6 a _ @ s 1 a _ @ s 1 a _ @ s 1 a _ @ s 1 a _ @ s 1 a _ @ s 1 a _ @ s 1 a _ @ s 1 a
20.去除字符串中最左或者最右对应的字符默认为“ ”:strip()
去除字符串中最左端对应字符组默认为“ ”:lstrip()
去除字符串中最右端对应字符组默认为“ ”:rstrip()
1 n = "ailadi"
2 s1 = n.strip("a")
3 s2 = n.rstrip("a")
4 s3 = n.lstrip("a")
5 s4 = n.strip("di")
6 print(s1)
7 print(s2)
8 print(s3)
9 print(s4)
10 结果:
11 iladi
12 ailadi
13 iladi
14 aila
21.创建两个字符串中的对应关系,和两个对应字符的转换:
创建:maktarns()
转换:tarnslate()
1 n1 = "abcde"
2 n2 = "12345"
3 s = str.maketrans(n1,n2)#建立对应关系
4 s1 = "aasdesfsfswdx"
5 ss = s1.translate(s)#进行转换
6 print(ss)
7 结果:
8 11s45sfsfsw4x
22.字符串分割split()、partition()
1 n = "ailadiailadi"
2 s1 = n.split("l")#默认全部寻找进行分割,无分割字符
3 s2 = n.split("l",1)#可以添加寻找次数
4 s3 = n.partition("l")#从左边开始寻找寻找一次,有分隔符
5 s4 = n.rpartition("l"))#从右边开始寻找寻找一次,有分隔符
6 print(s1)
7 print(s2)
8 print(s3)
9 print(s4)
10 结果:
11 ['ai', 'adiai', 'adi']
12 ['ai', 'adiailadi']
13 ('ai', 'l', 'adiailadi')
14 ('ailadiai', 'l', 'adi')
23.根据
进行分割splitlines()
1 n = "ailadi
ailadi"
2 s1 = n.splitlines(True)#打印
3 s2 = n.splitlines(False)#不打印
4 print(s1)
5 print(s2)
6 结果:
7 ['ailadi
', 'ailadi']
8 ['ailadi', 'ailadi']
24.字符串中大写换小写小写换大写swapcase()
1 n = "Ailadi"
2 s1 = n.swapcase()
3 print(s1)
4 结果:
5 aILADI
25.字符串的替换replace()
1 n = "ailadii"
2 s1 = n.replace("i","ii")#默认全部换掉
3 s2 = n.replace("i","ii",1)#可以写次数
4 print(s1)
5 print(s2)
6 结果:
7 aiiladiiii
8 aiiladii