文章目录
字符串方法定义:特性:python字符串内置方法1、capitalize()返回字符串的第一个大写字符首字符大写
2、center(width,(str))返回一个共 width 列、填充空格的字符串,原始字符串处于其中心位置。将某字符串居中,并以指定字符填充至指定长度。
3、count(str, beg=0, end=len(string))统计 str 在 string 中出现的次数,如果给定了开始索引 beg 和结束索引 end,将统计 str 在 string 中出现的次数。参数为字符串的一个子串,返回该子串出现的次数。
4、decode(encode='UTF-8', errors='strict')对 string 进行解码;如果发生错误,默认情况下会抛出 ValueError 异常,除非通过 ignore 或 replace 给出 errors。
5、encode(encode='UTF-8', errors='strict')对 string 进行解码;如果发生错误,默认情况下会抛出 ValueError 异常,除非通过 ignore 或 replace 给出 errors。
6、endswith(str, beg=0, end=len(string))确定 string 或 string的子串(如果给出了开始索引 beg 和结束索引 end)是否以 str 结尾,如果是则返回 True ,否则返回 False。
7、expandtabs(tabsize=8)在 string 中扩展制表符为多个空格;如果 tabsize 没有提供默认为 8 个空格。将字符串S中的 \t 替换为一定数量的空格。默认N=8。注意, expandtabs(8) 不是将 \t 直接替换为8个空格。例如 'xyz\tab'.expandtabs() 会将\t 替换为5个空格,因为"xyz"占用了3个字符位。另外,它不会替换换行符( \n 或 \r )时。
8、find(str, beg=0, end=len(string))确定 str 是否出现在 string 中;如果给定了开始索引 beg 和结束索引 end,则会确定 str 是否出现在 string子串中;如果找到则返回索引,否则返回 -1。返回值:索引值 或者 -1查找指定参数是否在字符串中,查找成功返回字符的索引下标,失败则返回-1。
9、format(*args, **kwargs)根据传入的 args 和 kwargs 进行字符串格式化。
10、index(str, beg=0, end=len(string))与 find() 相同,但如果未找到 str,则会抛出一个异常。返回值:索引值 或者 异常
11、isalnum()如果 string 中至少含有一个字符并且所有字符都是字母或数字,那么返回 True,否则返回 False。只包含数字或字母为True。
12、isalpha()如果 string 中至少含有一个字符并且所有字符都是字母,那么返回 True,否则返回 False。只包含字母为True。返回值:True 或 False
13、isdecimal()如果 string 中只包含十进制数则返回 True,否则返回 False。返回值:True 或 False。
14、isdigit()如果 string 中只包含数字则返回 True,否则返回 False只包含数字为true。返回值:True 或 False
15、islower()如果 string 包含至少一个区分大小写的字符并且都是小写返回 True,否则返回 False。均为小写为True。返回值:True 或 False
16、isnumeric()如果 stirng 只包含数字字符则返回 True,否则返回 False。返回值:True 或 False
17、isspace()如果 stirng 只包含空格字符则返回 True,否则返回 False。返回值:True 或 False
18、istitle()如果 string 是适合“标题大小写风格”(见 title())则返回 True,否则返回 False。单词首字母大写为True 。返回值:True 或 False
19、isupper()如果 string 包含至少一个区分大小写的字符并且都是大写返回 True,否则返回 False(均为大写为true)。都是大写为true。返回值:True 或 False
20、join(seq)将 seq 序列中的元素字符串合并(连接)到一个字符串,string 作为分隔符。以原字符串作为分隔符,插入到参数中每个字符之间。
21、ljust(width[, fillchar])返回一个空格填充的 string,原始字符的总列数为 width 的空间中左对齐。使用fillchar填充在字符串的右边,使得整体长度为width。如果不指定fillchar,则默认使用空格填充。如果width小于或等于字符串的长度,则无法填充,直接返回字符串S(不会创建新字符串对象)。
22、lower()将 string 中所有的大写字母转换为小写字母。
23、lstrip()删除 string 中所有前置空格。去掉字符串前面的空格,若加上参数,则改为删除字符串前的该参数
24、replace(str1, str2, num=string.count(str1))用 str2 替换 string 中出现的 str1,或者最多 num 个(如果给定 num 的值)。将字符串中的所有str1用str2替换,若使用count参数,则替换次数不超过count次。
25、rfind(str, beg=0, end=len(string))与 find() 相同,但在 string 中向后搜索。功能和find()一致,但查找方向从右边开始
26、rindex(str, beg=0, end=len(string))与 index() 相同,但在 string 中向后搜索。功能和index()一致,但查找方向从右边开始
27、rjust(width)返回一个空格填充的 string,原始字符的总列数为 width 的空间中右对齐。rjust() 则是填充在左边。如果不指定fillchar,则默认使用空格填充。如果width小于或等于字符串S的长度,则无法填充,直接返回字符串S(不会创建新字符串对象)。
28、rstrip()删除 string 中所有尾部空格。删除字符串末尾的空格,若加上参数,则改为删除字符串后的该参数。
29、split(str="", num=string.cont(str))根据分隔符 str (如果没有提供默认为空格) 分割 string 并返回子串的列表;如果给定了 num,则最多分为 num 个子串。以第一个参数为分隔符分割字符串,返回一个列表练习1:给定url,要求实现截取出“?”号后面的参数,并将参数以“key value”的键值对形式保存起来,并最终通过get(key)的方式可以取出对应的value值。
30、splitlines(num=string.count('n'))在所有(或 num 个)换行处分割 string 并返回一个删除换行符后每行的列表。splitlines()用来专门用来分割换行符。虽然它有点像 split('\n') 或 split('\r\n') ,但它们有些区别。splitlines() 中可以指定各种换行符,常见的是 \n 、 \r 、 \r\n 。如果指定keepends为True,则保留所有的换行符。将split()和splitlines()相比较一下
31、startswith(str, beg=0, end=len(string))确定 string 或 string的子串(如果给出了开始索引 beg 和结束索引 end)是否以 str 开始,如果是则返回 True,否则返回 False。
32、strip([obj])对 string 执行 lstrip() 和 rstrip() 操作默认删除字符串前后的空格,若加上参数,则改为删除字符串前后的该参数
33、swapcase()反转 string 中所有字母大小写(大写改小写,小写改大写)。
34、title()返回 string 的“标题大小写风格”版本,既所有单词都已大写字母开始,而其余字母小写(另外见 istitle())。
35、translate(str, del='')根据翻译表 str (256 个字符) 翻译 string,并删除 del 字符串中的内容。
36、upper()将 string 中所有的小写字母转换为大写字母。
37、zfill(width)返回左填充 0 并且总字数为 width 的原始字符串; 用于数字,zfill()保留任何给定的符号用0填充在字符串的左边使其长度为width。如果字符串前右正负号 +/- ,则0填充在这两个符号的后面,且符号也算入长度。如果width小于或等于字符串的长度,则无法填充,直接返回字符串本身(不会创建新字符串对象)。
字符串方法
python中字符串对象提供了很多方法来操作字符串,功能相当丰富。
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
这些方法的使用说明见 官方文档:.string methods ,本文对它们进行详细解释,各位以后可将本文当作手册。
这里没有模式匹配(正则)相关的功能。python中要使用模式匹配相关的方法操作字符串,需要 import re 导入re模块。关于正则模式匹配,参见: re Module Contents .。
注意,python中 字符串是不可变对象 ,所以所有修改和生成字符串的操作的实现方法都是另一个内存片段中新生成一个字符串对象。例如, ‘abc’.upper() 将会在划分另一个内存片段,并将返回的 ABC 保存在此内存中。
定义:
它是一个有序的字符的集合,用于存储和表示基本的文本信息,’ ‘或" "或’’’ ‘’'中间包含的内容称之为字符串。
特性:
只能存放一个值;不可变;按照从左到右的顺序定义字符集合,下标从0开始顺序访问,有序.
python字符串内置方法
1、capitalize()
返回字符串的第一个大写字符
首字符大写
>>> str0
= 'hello World'
>>> str0
.capitalize
()
'Hello world'
2、center(width,(str))
返回一个共 width 列、填充空格的字符串,原始字符串处于其中心位置。
将某字符串居中,并以指定字符填充至指定长度。
>>> "test".center
(20, '*')
'********test********'
3、count(str, beg=0, end=len(string))
统计 str 在 string 中出现的次数,如果给定了开始索引 beg 和结束索引 end,将统计 str 在 string 中出现的次数。
参数为字符串的一个子串,返回该子串出现的次数。
>>> print('xyabxyxy'.count
('xy'))
3
>>> print('xyabxyxy'.count
('xy',1))
2
>>> print('xyabxyxy'.count
('xy',1,7))
1
>>> print('xyabxyxy'.count
('xy',1,8))
2
4、decode(encode=‘UTF-8’, errors=‘strict’)
对 string 进行解码;如果发生错误,默认情况下会抛出 ValueError 异常,除非通过 ignore 或 replace 给出 errors。
5、encode(encode=‘UTF-8’, errors=‘strict’)
对 string 进行解码;如果发生错误,默认情况下会抛出 ValueError 异常,除非通过 ignore 或 replace 给出 errors。
6、endswith(str, beg=0, end=len(string))
确定 string 或 string的子串(如果给出了开始索引 beg 和结束索引 end)是否以 str 结尾,如果是则返回 True ,否则返回 False。
>>> str1
= 'Hello World'
>>> str1
.startswith
('h')
False
>>> str1
.endswith
('d')
True
7、expandtabs(tabsize=8)
在 string 中扩展制表符为多个空格;如果 tabsize 没有提供默认为 8 个空格。
将字符串S中的 \t 替换为一定数量的空格。默认N=8。
注意, expandtabs(8) 不是将 \t 直接替换为8个空格。例如 ‘xyz\tab’.expandtabs() 会将\t 替换为5个空格,因为"xyz"占用了3个字符位。
另外,它不会替换换行符( \n 或 \r )时。
>>> '01\t012\t0123\t01234'.expandtabs
(4)
'01 012 0123 01234'
>>> '01\t012\t0123\t01234'.expandtabs
(8)
'01 012 0123 01234'
>>> '01\t012\t0123\t01234'.expandtabs
(7)
'01 012 0123 01234'
>>> print('012\t0123\n01234'.expandtabs
(7))
012 0123
01234
8、find(str, beg=0, end=len(string))
确定 str 是否出现在 string 中;如果给定了开始索引 beg 和结束索引 end,则会确定 str 是否出现在 string子串中;如果找到则返回索引,否则返回 -1。
返回值:索引值 或者 -1
查找指定参数是否在字符串中,查找成功返回字符的索引下标,失败则返回-1。
>>> str1
= '123asdxfgs...'
>>> str1
.find
('123')
0
>>> str1
.find
('1234')
-1
>>> str2
= 'test88ooo88'
>>> str2
.find
('88')
4
>>> str2
.rfind
('88')
9
9、format(*args, **kwargs)
根据传入的 args 和 kwargs 进行字符串格式化。
tip:常与{}一起使用,参数个数由自己决定。
>>> "姓名:{}, 性别:{}".format('张三','男')
'姓名:张三, 性别:男'
>>> "姓名:{a}, 性别:{b}".format(b
= '男',a
= '张三')
'姓名:张三, 性别:男'
10、index(str, beg=0, end=len(string))
与 find() 相同,但如果未找到 str,则会抛出一个异常。
返回值:索引值 或者 异常
>>> str1
= '123asdxfgs...'
>>> str1
.index
('x')
6
>>> str1
.index
('b')
Traceback
(most recent call last
):
File
"<pyshell#33>", line
1, in <module
>
str1
.index
('b')
ValueError
: substring
not found
11、isalnum()
如果 string 中至少含有一个字符并且所有字符都是字母或数字,那么返回 True,否则返回 False。
只包含数字或字母为True。
>>> str1
= '123456789'
>>> str2
= '123asdxfgs...'
>>> str1
.isalpha
()
False
>>> str1
.isdigit
()
True
>>> str2
.isalnum
()
False
12、isalpha()
如果 string 中至少含有一个字符并且所有字符都是字母,那么返回 True,否则返回 False。
只包含字母为True。
返回值:True 或 False
>>> str1
= '123456789'
>>> str2
= '123asdxfgs...'
>>> str3
= 'abcdef'
>>> str1
.isalpha
()
False
>>> str2
.isalpha
()
False
>>> str3
.isalpha
()
True
13、isdecimal()
如果 string 中只包含十进制数则返回 True,否则返回 False。
返回值:True 或 False。
>>> str1
= "1234"
>>> str2
= "123a3b"
>>> str1
.isdecimal
()
True
>>> str2
.isdecimal
()
False
>>>
14、isdigit()
如果 string 中只包含数字则返回 True,否则返回 False
只包含数字为true。
返回值:True 或 False
>>> str1
= '123456789'
>>> str2
= '123asdxfgs...'
>>> str3
= 'abcdef'
>>> str1
.isdigit
()
True
>>> str2
.isdigit
()
False
>>> str3
.isdigit
()
False
15、islower()
如果 string 包含至少一个区分大小写的字符并且都是小写返回 True,否则返回 False。
均为小写为True。
返回值:True 或 False
>>> str1
= 'Hello World'
>>> str2
= '123asdxfgs...'
>>> str1
.isupper
()
False
>>> str1
.islower
()
False
>>> str2
.islower
()
True
16、isnumeric()
如果 stirng 只包含数字字符则返回 True,否则返回 False。
返回值:True 或 False
>>> str1
= '123456789'
>>> str2
= '123asdxfgs...'
>>> str3
= 'abcdef'
>>> str1
.isnumeric
()
True
>>> str2
.isnumeric
()
False
>>> str3
.isnumeric
()
False
17、isspace()
如果 stirng 只包含空格字符则返回 True,否则返回 False。
返回值:True 或 False
>>> str1
= 'aa aa'
>>> str2
= ' '
>>> str1
.isspace
()
False
>>> str2
.isspace
()
True
18、istitle()
如果 string 是适合“标题大小写风格”(见 title())则返回 True,否则返回 False。
单词首字母大写为True 。
返回值:True 或 False
>>> str1
= 'Hello World'
>>> str2
= 'Hello world'
>>> str1
.istitle
()
True
>>> str2
.istitle
()
False
19、isupper()
如果 string 包含至少一个区分大小写的字符并且都是大写返回 True,否则返回 False(均为大写为true)。
都是大写为true。
返回值:True 或 False
>>> str1
= 'Hello World'
>>> str2
= 'HELLO WORLD'
>>> str1
.isupper
()
False
>>> str2
.isupper
()
True
20、join(seq)
将 seq 序列中的元素字符串合并(连接)到一个字符串,string 作为分隔符。
以原字符串作为分隔符,插入到参数中每个字符之间。
>>> str_break
= 'xx'
>>> str_break
.join
('AB')
'AxxB'
>>> str_break
.join
('ABC')
'AxxBxxC'
21、ljust(width[, fillchar])
返回一个空格填充的 string,原始字符的总列数为 width 的空间中左对齐。
使用fillchar填充在字符串的右边,使得整体长度为width。如果不指定fillchar,则默认使用空格填充。
如果width小于或等于字符串的长度,则无法填充,直接返回字符串S(不会创建新字符串对象)。
>>> print('xyz'.ljust
(5,'_'))
xyz__
>>> print('xyz'.rjust
(5,'_'))
__xyz
22、lower()
将 string 中所有的大写字母转换为小写字母。
>>> str1
= 'Hello World'
>>> str1
.upper
()
'HELLO WORLD'
>>> str1
.lower
()
'hello world'
23、lstrip()
删除 string 中所有前置空格。
去掉字符串前面的空格,若加上参数,则改为删除字符串前的该参数
>>> str1
= ' content '
>>> str1
.lstrip
()
'content '
>>> str1
.rstrip
()
' content'
>>> str1
.strip
()
'content'
>>> str1
.strip
('t')
' content '
>>> str2
= '123and123'
>>> str2
.strip
('123')
'and'
>>> str2
.lstrip
('123')
'and123'
24、replace(str1, str2, num=string.count(str1))
用 str2 替换 string 中出现的 str1,或者最多 num 个(如果给定 num 的值)。
将字符串中的所有str1用str2替换,若使用count参数,则替换次数不超过count次。
>>> str1
= 'xxHxexlxxlxo Wxxxxorld'
>>> str1
.replace
('x', '')
'Hello World'
>>> str1
.replace
('x', 'A')
'AAHAeAlAAlAo WAAAAorld'
>>> str1
.replace
('x', 'A', 1)
'AxHxexlxxlxo Wxxxxorld'
25、rfind(str, beg=0, end=len(string))
与 find() 相同,但在 string 中向后搜索。
功能和find()一致,但查找方向从右边开始
>>> str1
= 'test88ooo88'
>>> str1
.find
('88')
4
>>> str1
.rfind
('88')
9
26、rindex(str, beg=0, end=len(string))
与 index() 相同,但在 string 中向后搜索。
功能和index()一致,但查找方向从右边开始
27、rjust(width)
返回一个空格填充的 string,原始字符的总列数为 width 的空间中右对齐。
rjust() 则是填充在左边。如果不指定fillchar,则默认使用空格填充。
如果width小于或等于字符串S的长度,则无法填充,直接返回字符串S(不会创建新字符串对象)。
>>> print('xyz'.ljust
(5,'_'))
xyz__
>>> print('xyz'.rjust
(5,'_'))
__xyz
28、rstrip()
删除 string 中所有尾部空格。
删除字符串末尾的空格,若加上参数,则改为删除字符串后的该参数。
>>> str1
= ' content '
>>> str1
.lstrip
()
'content '
>>> str1
.rstrip
()
' content'
>>> str1
.strip
()
'content'
>>> str1
.strip
('t')
' content '
>>> str2
= '123and123'
>>> str2
.strip
('123')
'and'
>>> str2
.lstrip
('123')
'and123'
29、split(str="", num=string.cont(str))
根据分隔符 str (如果没有提供默认为空格) 分割 string 并返回子串的列表;如果给定了 num,则最多分为 num 个子串。
以第一个参数为分隔符分割字符串,返回一个列表
>>> str1
= 'AxxBxxC'
>>> str1
.split
('xx')
['A', 'B', 'C']
>>> str1
.split
('x')
['A', '', 'B', '', 'C']
>>> str1
.split
('xxx')
['AxxBxxC']
>>> '1,2,3'.split
(',')
['1', '2', '3']
>>> '1,2,3'.split
(',',1)
['1', '2,3']
>>> '1,2,,3'.split
(',')
['1', '2', '', '3']
>>> '<hello><><world>'.split
('<')
['', 'hello>', '>', 'world>']
>>> '<hello><><world>'.split
('<>')
['<hello>', '<world>']
>>> '1 2 3'.split
()
['1', '2', '3']
>>> '1 2 3'.split
(maxsplit
=1)
['1', '2 3']
>>> ' 1 2 3 '.split
()
['1', '2', '3']
>>> ' 1 2 3 \n'.split
()
['1', '2', '3']
>>> ' 1 2 3 \n'.split
(' ')
['', '1', '', '2', '', '3', '', '\n']
>>> ' 1 2 3 \n'.split
('\t')
[' 1 2 3 \n']
>>> ' 1 2\n3 \n'.split
('\n')
[' 1 2', '3 ', '']
>>> ''.split
('\n')
['']
练习1:给定url,要求实现截取出“?”号后面的参数,并将参数以“key value”的键值对形式保存起来,并最终通过get(key)的方式可以取出对应的value值。
url = “http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0”
def f(str):
d
= {}
t
= str.split
("?")[1].split
("&")
for i
in range(len(t
)):
d
[t
[i
].split
("=")[0]] = t
[i
].split
("=")[1]
return d
print(f
(url
).get
("page_size"))
30、splitlines(num=string.count(‘n’))
在所有(或 num 个)换行处分割 string 并返回一个删除换行符后每行的列表。
splitlines()用来专门用来分割换行符。虽然它有点像 split(’\n’) 或 split(’\r\n’) ,但它们有些区别。
splitlines() 中可以指定各种换行符,常见的是 \n 、 \r 、 \r\n 。如果指定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']
将split()和splitlines()相比较一下
>>> ''.split
('\n')
['']
>>> 'One line\n'.split
('\n')
['One line', '']
>>> "".splitlines
()
[]
>>> 'Two lines\n'.splitlines
()
['Two lines']
31、startswith(str, beg=0, end=len(string))
确定 string 或 string的子串(如果给出了开始索引 beg 和结束索引 end)是否以 str 开始,如果是则返回 True,否则返回 False。
>>> str1
= 'Hello World'
>>> str1
.startswith
('h')
False
>>> str1
.endswith
('d')
True
32、strip([obj])
对 string 执行 lstrip() 和 rstrip() 操作
默认删除字符串前后的空格,若加上参数,则改为删除字符串前后的该参数
>>> str1
= ' content '
>>> str1
.strip
()
'content'
>>> str1
.strip
('t')
' content '
>>> str2
= '123and123'
>>> str2
.strip
('123')
'and'
33、swapcase()
反转 string 中所有字母大小写(大写改小写,小写改大写)。
>>> str1
= 'Hello World'
>>> str1
.swapcase
()
'hELLO wORLD'
34、title()
返回 string 的“标题大小写风格”版本,既所有单词都已大写字母开始,而其余字母小写(另外见 istitle())。
>>> print('ab XY'.title
())
Ab Xy
35、translate(str, del=’’)
根据翻译表 str (256 个字符) 翻译 string,并删除 del 字符串中的内容。
S
.translate
(table
)
static
str.maketrans
(x
[, y
[, z
]])
str.maketrans
() 生成一个字符一 一映射的table,然后使用 translate
(table
) 对字符串S中的每个字符进行映射。
如果你熟悉Linux,就知道tr命令,translate
()实现的功能和tr是类似的。
例如,现在想要对
"I love fairy"做一个简单的加密,将里面部分字符都替换为数字,这样别人就不知道转换后的这句话是什么意思。
>>> in_str
='abcxyz'
>>> out_str
='123456'
>>> map_table
=str.maketrans
(in_str
,out_str
)
>>> my_love
='I love fairy'
>>> result
=my_love
.translate
(map_table
)
>>> print(result
)
I love f1ir5
注意, maketrans
(x
[, y
[, z
]]) 中的x和y都是字符串,且长度必须相等。
如果 maketrans
(x
[, y
[, z
]]) 给定了第三个参数z,这个参数字符串中的每个字符都会被映射为
None。
例如,不替换
"o"和
"y"。
>>> in_str
='abcxyz'
>>> out_str
='123456'
>>> map_table
=str.maketrans
(in_str
,out_str
,'ay')
>>> my_love
='I love fairy'
>>> result
=my_love
.translate
(map_table
)
>>> print(result
)
I love fir
36、upper()
将 string 中所有的小写字母转换为大写字母。
>>> str1
= 'Hello World'
>>> str1
.upper
()
'HELLO WORLD'
>>> str1
.lower
()
'hello world'
37、zfill(width)
返回左填充 0 并且总字数为 width 的原始字符串; 用于数字,zfill()保留任何给定的符号
用0填充在字符串的左边使其长度为width。如果字符串前右正负号 +/- ,则0填充在这两个符号的后面,且符号也算入长度。
如果width小于或等于字符串的长度,则无法填充,直接返回字符串本身(不会创建新字符串对象)。
>>> print('abc'.zfill
(5))
00abc
>>> print('-abc'.zfill
(5))
-0abc
>>> print('+abc'.zfill
(5))
+0abc
>>> print('42'.zfill
(5))
00042
>>> print('-42'.zfill
(5))
-0042
>>> print('+42'.zfill
(5))
+0042
参考链接