关于 Python 的字符串处理相关的方法还是非常多的,由于我正在学习 Python,于是就把 Python 中这些混杂的用于 string 的函数总结出来,在自己忘记的时候便于查找,希望对于有类似需求的人有所帮助。
将一个字符串的第一个字母大写
str.captalize()
无
string
str = "hello world!"print "str.capitalize(): ", str.capitalize()
tr.capitalize(): Hello world!
将字符串居中,居中后的长度为 width
将字符串居中,居中后的长度为 width
str.center(width[, fillchar])
width: 表示字符串总长度
fillchar: 使字符串居中所填充的字符,默认为空格
返回填充字符后的字符串
str = "hello world!"print "str.center(20): ", str.center(20)print "str.center(20,'-'): ", str.center(20,'-')
tr.center(20): hello world!str.center(20,'-'): ----hello world!----
返回该字符串中出现某字符串序列(或字符)的次数
str.count(sub, start=0, end=len(string))
sub: 被查找的字符串序列
start: 开始查找的索引位置,默认为字符串开始
end: 结束查找的索引位置,默认为字符串结束
被查找的序列在字符串的查找位置中出现的次数
str = "hello world! hello world!"sub = "o"print "str.count(sub): ", str.count(sub) sub = "hello"print "str.count(sub, 5) ", str.count(sub, 5)
str.count(sub): 4str.count(sub, 5) 1
decode(encoding=’UTF-8’,errors=’strict’) 函数 & encode(encoding=’UTF-8’,errors=’strict’)
使用特定编码将字符串解码(decode)/编码(encode)
str.decode(encoding='UTF-8',errors='strict')
str.encode(encoding='UTF-8',errors='strict')
encoding: 使用的编码格式
errors: 设置不同的错误处理方法,其他选项有 ignore, replace, xmlcharrefreplace, backslashreplace
编码/解码后的字符串
str = "hello world!"str = str.encode('base64', 'strict')print "Encoded str: ", strprint "Decoded str: ", str.decode('base64')
Encoded str: aGVsbG8gd29ybGQh Decoded str: hello world!
判断字符串是否是以某字符串结尾的
str.endswith(suffix, start=0, end=len(string))
suffix: 被查找的字符串
start: 字符串查找的起始位置,默认为字符串起始位置
end: 字符串查找的结束位置,默认为字符串结束位置
如果字符串是以 suffix 结尾的返回 True, 否则返回 False
str = "hello world!"suffix = "world!"print str.endswith(suffix) suffix = "llo"print str.endswith(suffix,0,4)print str.endswith(suffix,0,5)
TrueFalseTrue
提供自定义 tab(/t) 长度的方法,默认为8
str.expandtabs(tabsize=8)
tabsize: 表示自定义 tab 的长度
string
str = "hellotworld!"print "Original str: " + strprint "Defalut expanded tab: " + str.expandtabs();print "Double expanded tab: " + str.expandtabs(16)
Original str: hello world! Defalut expanded tab: hello world! Double expanded tab: hello world!
在字符串的某指定位置查找某字符串
str.find(str, start=0, end=len(string))
str: 被查找的子字符串
start: 查找的起始位置,默认为字符串起始位置
end: 查找的结束位置,默认为字符串结束位置
如果查找到,返回该子字符串的索引;未查找到,返回-1
str = "hello world!"str1 = "wo"print str.find(str1)print str.find(str1, 8)
6-1
功能上与 find() 相同,只是在未找到子字符串是抛出异常
str.index(str, start=0, end=len(string))
同 find()
如果查找到,返回该子字符串的索引;未查找到,抛出异常
str = "hello world!"str1 = "wo"print str.index(str1)print str.index(str1, 8)
6 Traceback (most recent call last): File "teststrmethods.py", line 6, inprint str.index(str1, 8) ValueError: substring not found
判断该字符串是否只是字母数字组合
str.isalnum()
无
如果该字符串是字母数字组合,返回 True,否则返回 False
str = "helloworld"print str.isalnum() str = "hello world"print str.isalnum() str = "hello123"print str.isalnum() str = "hello123!"print str.isalnum()
TrueFalseTrueFalse
判断该字符串是否是字母组合
str.isalpha()
无
如果该字符串是字母组合,返回 True,否则返回 False
str = "helloworld"print str.isalpha() str = "hello world"print str.isalpha() str = "hello123"print str.isalpha() str = "hello123!"print str.isalpha()
TrueFalseFalseFalse
判断该字符串是否只包含数字
str.isdigit()
无
如果该字符串只包含数字,则返回 True,否则返回 False
str = "hello123"print str.isdigit() str = "123456"print str.isdigit()
FalseTrue
判断该字符串中是否只是小写字母
str.islower()
无
如果该字符串中只是小写字母,返回True,否则返回False
str = "hello wolrd!"print str.islower() str = "Hello Wolrd!"print str.islower()
TrueFalse
e() 函数
判断该字符串是否只包含空格
str.isspace()
无
如果该字符串只包含空格,返回True,否则返回False
str = " "print str.isspace() str = "Hello Wolrd!"print str.isspace()
TrueFalse
检查该字符串中的单词是否首字母都大写
str.istitle()
无
如果该字符串中的单词首字母都大写了,返回True,否则返回False
str = "Hello world!"print str.istitle() str = "Hello Wolrd!"print str.istitle()
FalseTrue
判断该字符串中的字母是否都是大写
str.isupper()
无
如果该字符串中的字母都是大写,返回True,否则返回False
str = "Hello world!"print str.isupper() str = "HELLO WORLD!"print str.isupper()
FalseTrue
用该字符串连接某字符序列(seq)
str.join(sequence)
sequence: 被连接的字符序列
返回连接之后的字符串
str = "-"sequence = ("hello", "world", "everyone", "!")print str.join(sequence)
hello-world-everyone-!
得到该字符串的长度
len(str)
无
返回该字符串长度
str = "Hello world!"print "the length of str: ", len(str)
the length of str: 12
在字符串的右边填充字符使得字符串达到指定长度
str.ljust(width, fillchar=' ')
width: 填充后的目标长度
fillchar: 用于填充的字符,默认为空格
返回填充后的字符串
str = "Hello world"print str.ljust(15)print str.ljust(15,'!')
Hello world Hello world!!!!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4328/viewspace-2801920/ ,如需转载,请注明出处,否则将追究法律责任。