str的方法使用

capitalize()把字符串进行首字母大写:

test = 'drocess finished with exit code 0'
v = test.capitalize()
print(v)

输出:
Drocess finished with exit code 0

title()把字符串标题化(每个单词首字母大写)

upper()把每个字符转化为大写

lower()把每个字符转化为小写

test ='drocess finished with exit code 0 '

v1 = test.title()
print(v1)

输出:
Drocess Finished With Exit Code 0

swapcase()交换字母的大小写

test = 'Alex'

v = test.swapcase()
print(v)

istitle()判断字符串是否是标题(每个单词首字母大写)

isupper()判断字符串中的字母是否全是大写

islower()判断字符串中的字母是否全是小写

test ='Process finished with exit code 0 '

v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)

输出:
False
Process Finished With Exit Code 0
True

casefold()和lower()把字符串转换成小写,前者更倾向国际使用:

test = 'ALEX'
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)

输出:
alex
alex

center()设置字符串居中

ljust()设置字符串靠左

rjust()设置字符串靠右

20代表总宽度,*代表空白位置填充

test = 'Lex'
v3 = test.center(20,'*')
print(v3)
输出:
********Lex*********

count()寻找字符串中的子序列的个数,从第索引5-20内找

test = 'aLExgoog'
v3 = test.count('g',5,20)
print(v3)

输出:
1

encode()

decode()

endswith(),startswith(),字符串是否以子序列开始或结尾

test = 'aLExgoog'

v3 = test.endswith('g')
print(v3)

v2 = test.startswith('e')
print(v2)

输出:
True
False

find()从位置5-12查找字符串中的第一个子序列的位置,-1表示未找到

rfind()从位置12-5找字符串中的倒数第一个子序列的位置,-1表示未找到

test = 'Alexdgdagdfdsafdaf'

v = test.find("d",5,12)
print(v)
v2 = test.rfind('d',5,12)
print(v2)
输出:

6
11

format()格式化,将字符串中的占位符替换为指定的值,用变量或者索引

test = 'I am {name},age is {a}'  #用变量name,a

v3 = test.format(name='malin',a=19)
print(v3)

输出:

I am malin,age is 19

test = 'I am {0},age is {1}'  # 用索引

v3 = test.format('malin',19)
print(v3)

输出:
I am malin,age is 19

format_map()格式化,将字符串中的占位符替换为指定的值,用字典

test = 'I am {name},age is {a}'

v3 = test.format_map({'name':'malin','a':19})
print(v3)

输出:
I am malin,age is 19

isalnum()判断字符串的字符是否全是字母或中文或数字

test = '我是n9780'

v3 = test.isalnum()
print(v3)

输出:
True

isalpha()判断字符串的字符是否全是字母或者中文

test = 'L我是ling'

v3 = test.isalpha()
print(v3)

输出:
True

expandtabs()以长度20为单位(遇到制表符\t时补充空格到20)制作表格.

test = 'username\temail\tpasswd\nmalin\[email protected]\t123\t'

v3 = test.expandtabs(20)
print(v3)

isdecimal(),isdigit(),isnumeric判断字符串的字符是否全是数字

isdecimal()只能判断阿拉伯数字,比如2
isdigit还能判断符号数字,比如②
isnumeric还能判断中文数字,比如二

test = '二'          #或test='②'
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)

输出:
False False True

isidentifier()判断字符串是否是标识符

** 标识符:字母,数字,下划线,且第一个字符不能为数字**

test = '2fsaa_f123'

v1 = test.isidentifier()
print(v1)

输出:
False

isprintable()判断字符串的字符是否包含转义过的字符

test = '电饭锅\t'

v1 = test.isprintable()
print(v1)

输出:
False

isspace()判断字符串是否全是空格

test = '   '

v1 = test.isspace()
print(v1)

输出:
True

join()将字符串t加入到字符串test中的每一个字符之间

test ='你是风儿我是沙'
print(test)
t = '++++++'
v1 = t.join(test)
print(v1)

输出:
你是风儿我是沙
你++++++是++++++风++++++儿++++++我++++++是++++++沙

例2:

'.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'

lstrip()字符串左边依次去除匹配到的字符,直到不匹配

rstrip()字符串右边依次去除匹配到的字符,直到不匹配

strip()字符串两边依次去除匹配到的字符,直到不匹配

当没有参数时可去除空白部分

test ='abcdefg123'
v1 = test.lstrip('ae3b')
v2 = test.rstrip('ae3b')
v3 = test.strip('ae3b')
print(v1)
print(v2)
print(v3)

输出:
cdefg123
abcdefg12
cdefg12

maketrans()建立字符串对应表,translate()依照对应表翻译字符串.

test = 'abcdefghij'
test1 = '1234567890'
v = 'ldifgalfllif'
m = str.maketrans(test,test1)
print(v.translate(m))

输出:
l49671l6ll96

partition()从左边遇到的第一个参数把字符串分割成3部分,结果是一个元祖

test = 'www.baidu.com'

v = test.partition('.')
print(v)

输出:
(‘www’, ‘.’, ‘baidu.com’)

rpartition()从右边遇到的第一个参数把字符串分割成3部分,结果是一个元祖

test = 'www.baidu.com'

v = test.rpartition('.')
print(v)

输出:
(‘www.baidu’, ‘.’, ‘com’)

split()从左边开始用参数".“将字符串分割1次,参数”."不保留,不写1表示全部分割,结果是一个列表

test = 'www.baidu.com'

v = test.split('.',1)
print(v)

v2 = test.split('.')
print(v2)

输出:
[‘www’, ‘baidu.com’]
[‘www’, ‘baidu’, ‘com’]

rsplit()从右边开始用参数s将字符串分割3次,参数s不保留,不写3表示全部分割

test = 'www.baidu.com'

v = test.rsplit('.',1)
print(v)

输出:
[‘www.baidu’, ‘com’]

replace()把字符串中的字符s替换成a,共替换3次

test = 'sssssbbbbbb'
v = test.replace('s','a',3)
print(v)

输出:
aaassbbbbbb

猜你喜欢

转载自blog.csdn.net/weixin_43760098/article/details/84346421
今日推荐