转载须注明出处:@Orca_J35 | GitHub@orca-j35
字符串不仅支持所有通用序列操作,还实现了很多附件方法。
我会以『字符串方法』为标题,分几篇笔记逐一介绍这些方法。
我会在这仓库中持续更新笔记:https://github.com/orca-j35/python_notes
expandtabs
str.expandtabs(tabsize=8)
Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every tabsize characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab (\t
), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline (\n
) or return (\r
), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed.
# 将字符串中的所有制表符替换空格,空格的数量取决于当前列的字符数和tablesize
# tabsize用于指定列宽,每列的宽度需等于tabsize,
# 若当前列的实际宽度<tabsize,剩余部分用0填充;
# 若当前列的实际长度≥tabsize,则将则当前列的宽度增加至tabsize的倍数,剩余部分用0填充
# 比如'01\t012\t0123\t01234',有4列:01/012/0123/01234
'01\t012\t0123\t01234'.expandtabs()
# '01 012 0123 01234'
# '01234567012345670123456701234567'每列的宽度是8
'01\t012\t0123\t01234'.expandtabs(4)
# '01 012 0123 01234'
# '012301230123012301230123'每列的宽度是4
# 如果遇到'\n'或'\r',则会重新计算列宽
'01\t012\t0123\n01\t01234'.expandtabs()
# '01 012 0123\n01 01234'
# '01234567012345670123450123456701234567'每列的宽度是8
center
str.center(width[, fillchar])
Return centered in a string of length width. Padding is done using the specified fillchar(default is an ASCII space). The original string is returned if width is less than or equal to len(s)
.
# 以指定宽度width,对字符串进行居中
>>> 'orca_j35'.center(20)
' orca_j35 '
# fillchar用于指定填充字符,默认是ASCII空格
>>> 'orca_j35'.center(20,'*')
'******orca_j35******'
# 如果width≤len(s),则返回原字符串
>>> 'orca_j35'.center(1)
'orca_j35'
ljust
str.ljust(width[, fillchar])
Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s)
.
# 以指定宽度width,对字符串进行左对齐
>>> 'orca_j35'.ljust(20)
'orca_j35 '
# fillchar用于指定填充字符,默认是ASCII空格
>>> 'orca_j35'.ljust(20,'*')
'orca_j35************'
# 如果width≤len(s),则返回原字符串
>>> 'orca_j35'.ljust(1)
'orca_j35'
rjust
str.rjust(width[, fillchar])
Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s)
.
# 以指定宽度width,对字符串进行右对齐
>>> 'orca_j35'.rjust(20)
' orca_j35'
# fillchar用于指定填充字符,默认是ASCII空格
>>> 'orca_j35'.rjust(20,'*')
'************orca_j35'
# 如果width≤len(s),则返回原字符串
>>> 'orca_j35'.rjust(1)
'orca_j35'
zfill
str.zfill(width)
Return a copy of the string left filled with ASCII '0'
digits to make a string of length width. A leading sign prefix ('+'
/'-'
) is handled by inserting the padding after the sign character rather than before. The original string is returned if width is less than or equal to len(s)
.
For example:
# 在字符串左侧填充'0',直至字符串的长度为width
>>> "42".zfill(5)
'00042'
# '+'/'-'会在'0'之前
>>> "-42".zfill(5)
'-0042'
>>> "+42".zfill(5)
'+0042'
>>> '--'.zfill(5)
'-000-'
>>> ' '.zfill(5)
'0000 '
>>> ''.zfill(5)
'00000'
# 其余符号会在'0'之后
>>> "=42".zfill(5)
'00=42'
# 如果width≤len(s),则返回原字符串
>>> 'orca_j35'.zfill(5)
'orca_j35'
format
str.format(*args, **kwargs)
str.format()
和string.Formatter
类均采用格式化字符串语法(Format String Syntax),该该语法和 f-string 的语法相似,但存在一些区别。有关格式化字符串语法的详细信息可阅读『string — Common string operations.md』。f-string、
string .format()
、string.Formatter
使用相同的格式化说明符(format specifier mini-language),详见笔记『string — Common string operations.md』-> "Format Specification Mini-Language"。
该方法用于执行字符串格式化操作,字符串对象中需包含:文本字面值和替换字段。替换字段是指被置于花括弧(curly braces) {}
中的表达式,其中可包含位置实参的数值索引,或包含关键字实参的名称。str.format
方法会基于当前字符串创建一个新副本,并用实参替换掉副本中对应的字段。
>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'
有关格式化字符串的详细语法,请阅读 Format String Syntax;str.format
与 string.Formatter
拥有相同的实现方式,如果想了解 str.format
的实现方式 ,可以参考『string — Common string operations.md』-> "Custom String Formatting"。
Note - When formatting a number (
int
,float
,complex
,decimal.Decimal
and subclasses) with then
type (ex:'{:n}'.format(1234)
), the function temporarily sets theLC_CTYPE
locale to theLC_NUMERIC
locale to decodedecimal_point
andthousands_sep
fields oflocaleconv()
if they are non-ASCII or longer than 1 byte, and theLC_NUMERIC
locale is different than theLC_CTYPE
locale. This temporary change affects other threads.
Changed in version 3.7: When formatting a number with the n
type, the function sets temporarily the LC_CTYPE
locale to the LC_NUMERIC
locale in some cases.
format_map
str.format_map(mapping)
Similar to str.format(**mapping)
, except that mapping
is used directly and not copied to a dict
. This is useful if for example mapping
is a dict subclass:
# 与str.format(**mapping)的区别在于无需对mapping进行解包
>>> student = dict(name='Joy',age=3)
>>> 'My name is {name},i am {age} old'.format_map(student)
'My name is Joy,i am 3 old'
>>> 'My name is {name},i am {age} old'.format(**student)
'My name is Joy,i am 3 old'
This is useful if for example mapping
is a dict subclass:
>>> class Default(dict):
... def __missing__(self, key):
... return key
...
>>> '{name} was born in {country}'.format_map(Default(name='Guido'))
'Guido was born in country'
New in version 3.2.