好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python的内置字符串方法分析

本文主要给大家介绍了下Python的一些内置字符串的方法,包括概览,字符串大小写转换,字符串格式 输出,字符串搜索定位与替换,字符串的联合与分割,字符串条件判断,字符串编码

'dog wow wow jiao'.rpartition('wow')
Out[131]: ('dog wow ', 'wow', ' jiao')

'dog wow wow jiao'.rpartition('dog')
Out[132]: ('', 'dog', ' wow wow jiao')

'dog wow wow jiao'.rpartition('jiao')
Out[133]: ('dog wow wow ', 'jiao', '')

'dog wow wow jiao'.rpartition('ww')
Out[135]: ('', '', 'dog wow wow jiao')
str.split(sep=None, maxsplit=-1); str.rsplit(sep=None, maxsplit=-1)
'1,2,3'.split(','), '1, 2, 3'.rsplit()
# (['1', '2', '3'], ['1,', '2,', '3'])

'1,2,3'.split(',', maxsplit=1), '1,2,3'.rsplit(',', maxsplit=1)
# (['1', '2,3'], ['1,2', '3'])

'1 2 3'.split(), '1 2 3'.rsplit()
# (['1', '2', '3'], ['1', '2', '3'])

'1 2 3'.split(maxsplit=1), '1 2 3'.rsplit(maxsplit=1)
# (['1', '2 3'], ['1 2', '3'])

' 1 2 3 '.split()
# ['1', '2', '3']

'1,2,,3,'.split(','), '1,2,,3,'.rsplit(',')
# (['1', '2', '', '3', ''], ['1', '2', '', '3', ''])

''.split()
# []
''.split('a')
# ['']
'bcd'.split('a')
# ['bcd']
'bcd'.split(None)
# ['bcd']
str.splitlines([keepends])

字符串以行界符为分隔符拆分为列表;当 keepends 为True,拆分后保留行界符,能被识别的行界符见官方文档。

'ab c\n\nde fg\rkl\r\n'.splitlines()
# ['ab c', '', 'de fg', 'kl']
'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
# ['ab c\n', '\n', 'de fg\r', 'kl\r\n']

"".splitlines(), ''.split('\n') #注意两者的区别
# ([], [''])
"One line\n".splitlines()
# (['One line'], ['Two lines', ''])

字符串条件判断

str.endswith(suffix[, start[, end]]); str.startswith(prefix[, start[, end]])
text = 'outer protective covering'

text.endswith('ing')
# True

text.endswith(('gin', 'ing'))
# True
text.endswith('ter', 2, 5)
# True

text.endswith('ter', 2, 4)
# False

str.isalnum()

字符串和数字的任意组合,即为真,简而言之:

只要 c.isalpha(), c.isdecimal(), c.isdigit(), c.isnumeric() 中任意一个为真,则 c.isalnum() 为真。

'dobi'.isalnum()
# True

'dobi123'.isalnum()
# True

'123'.isalnum()
# True

'徐'.isalnum()
# True

'dobi_123'.isalnum()
# False

'dobi 123'.isalnum()
# False

'%'.isalnum()
# False
str.isalpha()
Unicode 字符数据库中作为 “Letter”(这些字符一般具有 “Lm”, “Lt”, “Lu”, “Ll”, or “Lo” 等标识,不同于 Alphabetic) 的,均为真。

'dobi'.isalpha()
# True

'do bi'.isalpha()
# False

'dobi123'.isalpha()
# False

'徐'.isalpha()
# True
str.isdecimal(); str.isdigit(); str.isnumeric()
三个方法的区别在于对 Unicode 通用标识的真值判断范围不同:

isdecimal: Nd,
isdigit: No, Nd,
isnumeric: No, Nd, Nl

digit 与 decimal 的区别在于有些数值字符串,是 digit 却非 decimal ,具体戳 这里

num = '\u2155'
print(num)
# ?
num.isdecimal(), num.isdigit(), num.isnumeric()
# (False, False, True)

num = '\u00B2'
print(num)
# 2
num.isdecimal(), num.isdigit(), num.isnumeric()
# (False, True, True)

num = "1" #unicode
num.isdecimal(), num.isdigit(), num.isnumeric()
# (Ture, True, True)

num = "'Ⅶ'"
num.isdecimal(), num.isdigit(), num.isnumeric()
# (False, False, True)

num = "十"
num.isdecimal(), num.isdigit(), num.isnumeric()
# (False, False, True)

num = b"1" # byte
num.isdigit() # True
num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'
str.isidentifier()

判断字符串是否可为合法的标识符。

'def'.isidentifier()
# True

'with'.isidentifier()
# True

'false'.isidentifier()
# True

'dobi_123'.isidentifier()
# True

'dobi 123'.isidentifier()
# False

'123'.isidentifier()
# False
str.islower()
'徐'.islower()
# False

'?'.islower() #德语大写字母
# False

'a徐'.islower()
# True

'ss'.islower()
# True

'23'.islower()
# False

'Ab'.islower()
# False

str.isprintable()

判断字符串的所有字符都是可打印字符或字符串为空。Unicode 字符集中 “Other” “Separator” 类别的字符为不可打印的字符(但不包括 ASCII 的空格(0x20))。

'dobi123'.isprintable()
# True

'dobi123\n'.isprintable()
Out[24]: False

'dobi 123'.isprintable()
# True

'dobi.123'.isprintable()
# True

''.isprintable()
# True

str.isspace()

判断字符串中是否至少有一个字符,并且所有字符都是空白字符。

In [29]: '\r\n\t'.isspace()
Out[29]: True

In [30]: ''.isspace()
Out[30]: False

In [31]: ' '.isspace()
Out[31]: True

str.istitle()

判断字符串中的字符是否是首字母大写,其会忽视非字母字符。

'How Python Works'.istitle()
# True

'How Python WORKS'.istitle()
# False

'how python works'.istitle()
# False

'How Python Works'.istitle()
# True

' '.istitle()
# False

''.istitle()
# False

'A'.istitle()
# True

'a'.istitle()
# False

'甩甩Abc Def 123'.istitle()
# True
str.isupper()
'徐'.isupper()
# False

'DOBI'.isupper()
Out[41]: True

'Dobi'.isupper()
# False

'DOBI123'.isupper()
# True

'DOBI 123'.isupper()
# True

'DOBI\t 123'.isupper()
# True

'DOBI_123'.isupper()
# True

'_123'.isupper()
# False

字符串编码

str.encode(encoding="utf-8", errors="strict")

fname = '徐'

fname.encode('ascii')
# UnicodeEncodeError: 'ascii' codec can't encode character '\u5f90'...

fname.encode('ascii', 'replace')
# b'?'

fname.encode('ascii', 'ignore')
# b''

fname.encode('ascii', 'xmlcharrefreplace')
# b'徐'

fname.encode('ascii', 'backslashreplace')
# b'\\u5f90'

更多Python的内置字符串方法分析相关文章请关注PHP中文网!

查看更多关于Python的内置字符串方法分析的详细内容...

  阅读:37次