encode(encoding='utf-8', errors='strict')
- 释义
encoding
: 指定编码格式,默认为utf-8
errors
- 设置不同错误的处理方案,默认为
strict
,意为编码错误引起一个UnicodeError
- 其他的值:
ignore
,replace
,xmlcharrefreplace
,backslashreplace
- 还可以通过
codecs.register_error()
注册值
- 设置不同错误的处理方案,默认为
>>> s = "YorkFish不是鱼"
>>> utf8 = s.encode()
>>> utf8
b'YorkFishxe4xb8x8dxe6x98xafxe9xb1xbc'
>>> utf8.decode()
'YorkFish不是鱼'
>>>
>>> gbk = s.encode("gbk")
>>> gbk
b'YorkFishxb2xbbxcaxc7xd3xe3'
>>> gbk.decode("gbk")
'YorkFish不是鱼'
>>>
expandtabs([tabsize=8])
- 释义:
- 将字符串中的
- 如果不指定参数,默认 1 个制表符转 8 个空格
- 将字符串中的
>>> s = "a tab: "
>>> s
'a tab: '
>>> s.expandtabs()
'a tab: '
>>> s.expandtabs(16)
'a tab: '
>>>
顺便补上 print()
print(value, ..., sep=' ', end=' ', file=sys.stdout, flush=False)
- 默认情况下,将值打印到流或
sys.stdout
- 关键字参数
file
: 类似于文件的对象(流);默认为当前的sys.stdout
sep
: 在值之间插入的字符串,默认为空格end
: 在最后一个值后追加的字符串,默认为换行符flush
:whether to forcibly flush the stream
是否强制刷新数据流
first = "York"
last = "Fish"
print(first + last)
print(first, last, sep='')
print(first, last)
print()
print("YorkFish", end='
')
print(first, end='-')
print(last)
>>>
YorkFish
YorkFish
York Fish
YorkFish
York-Fish
format_map()
- 释义:在给定的字典中找对应的值
例1 官网的例子
class Default(dict):
def __missing__(self, key):
return key
print('{name} was born in {country}'.format_map(Default(name='Guido')))
>>>
Guido was born in country
例2
>>> d = {"name": "YorkFish", "language": "Python"}
>>> "{name} is learning {language}.".format_map(d)
'YorkFish is learning Python.'
>>>
replace(old, new[, count])
- 释义
- 用
new
替换old
,有多少换多少 - 如果指定
count
,则替换不超过count
次
- 用
>>> s = "All things in their being are good for something."
>>> s.replace('i', 'I')
'All thIngs In theIr beIng are good for somethIng.'
>>> s.replace('i', 'I', 3)
'All thIngs In theIr being are good for something.'
>>>
split(sep=None, maxsplit=-1)
- 释义
- 以
sep
分割字符串 - 默认以空格为分割符,有个陷阱,见例2
- 如果设置了
maxsplit
,则仅分割maxsplit
次
- 以
例1
>>> "Y.o.r.k.F.i.s.h".split('.')
['Y', 'o', 'r', 'k', 'F', 'i', 's', 'h']
>>> "Y.o.r.k.F.i.s.h.".split('.')
['Y', 'o', 'r', 'k', 'F', 'i', 's', 'h', '']
>>> "Y.o.r.k.F.i.s.h".split('.', maxsplit=4)
['Y', 'o', 'r', 'k', 'F.i.s.h']
>>>
例2
>>> " yorkfisih".split()
['yorkfisih']
>>>
>>> " yorkfisih".split(' ')
['', 'yorkfisih']
>>>
>>> ''.split(' ') # n 个空格会被分为 n+1 个部分
['']
>>> ' '.split(' ')
['', '']
>>> ' '.split(' ')
['', '', '']
>>>
rsplit(sep=None, maxsplit=-1)
用法类似,从右边开始分割
splitlines([keepends])
- 释义
- 按照
- 若
keepends
为True
,则保留换行符
- 按照
例1
>>> s = "Cease to struggle
and you cease to live.
(Thomas Carlyle)"
>>> s.splitlines()
['Cease to struggle ', 'and you cease to live.', '(Thomas Carlyle)']
>>> s.splitlines(True)
['Cease to struggle
', 'and you cease to live.
', '(Thomas Carlyle)']
>>>
例2 splitlines() 与 split()
>>> ''.splitlines()
[]
>>> ''.split('
')
['']
>>> "love at first sight.
".splitlines()
['love at first sight.']
>>> "love at first sight.
".split('
')
['love at first sight.', '']
startswith(prefix[, start[, end]])
- 释义
- 检查字符串是否以指定前缀
prefix
开头 - 返回
True
orFalse
- 检查字符串是否以指定前缀
>>> s = "The best is yet to come."
>>> s.startswith("The")
True
>>> s.startswith("is", 9)
True
>>> s.startswith("best", 4, 8) # [4, 8)
True
>>> s.startswith("best", 4, 7)
False
>>>
endswith(sub[, start[, end]])
见第一弹
swapcase()
- 释义:翻转字符串中的大小写
>>> "YorkFish".swapcase()
'yORKfISH'
>>>
title()
- 释义
- 返回“标题化”的字符串
- Python 只是单纯地使每个词的首字母大写,非首字母小写
- 对标题而言,比如介词要保持原样,这个
title()
没有实现
>>> "the great gatsby".title()
'The Great Gatsby'
>>>
zfill(width)
- 释义
- 必须填
width
,否则会抛异常 - 返回长度为
width
的字符串 - 原字符串右对齐
- 若
width
大于字符串长度,前边用 0 填充
- 必须填
>>> name = "YorkFish"
>>> name.zfill(5)
'YorkFish'
>>> name.zfill(10)
'00YorkFish'