目录
encoding
指定的编码格式对字符串进行编码,默认为 utf-8
,不区分大小写errors
用于设置不同错误的处理方案,默认为 ‘strict‘,意为编码错误引起一个UnicodeErrorerrors
其他可能的值有:‘ignore‘, ‘replace‘, ‘xmlcharrefreplace‘, ‘backslashreplace‘
以及通过 codecs.register_error()
注册的任何值# 例1
str1 = "YorkFish不是鱼"
str_utf8 = str1.encode("UTF-8")
str_gbk = str1.encode("GBK")
print("编码前: ", str1, end="\n\n") # 设置 print 结尾为两个回车,方便阅读
print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk, end="\n\n")
print("utf-8 默认解码:", str_utf8.decode()) # 默认 encoding=‘utf-8‘,errors=‘strict‘
print("utf-8 解码:", str_utf8.decode(‘utf-8‘,‘strict‘)) # utf-8 不区分大小写
print("UTF-8 解码:", str_utf8.decode(‘UTF-8‘,‘strict‘))
print("GBK 解码:", str_gbk.decode(‘GBK‘,‘strict‘)) # gbk 也不区分大小写
编码前: YorkFish不是鱼
UTF-8 编码: b‘YorkFish\xe4\xb8\x8d\xe6\x98\xaf\xe9\xb1\xbc‘
GBK 编码: b‘YorkFish\xb2\xbb\xca\xc7\xd3\xe3‘
utf-8 默认解码: YorkFish不是鱼
utf-8 解码: YorkFish不是鱼
UTF-8 解码: YorkFish不是鱼
GBK 解码: YorkFish不是鱼
Tab
符号,即 \t
,转换为空格# 例2
str2 = "制表符攻击:\t!!!"
print ("替换 \\t 符号前: " + str2)
print ("替换 \\t 符号后: " + str2.expandtabs())
print ("用 16 个空格替换 \\t 符号: " + str2.expandtabs(16))
替换 \t 符号前: 制表符攻击: !!!
替换 \t 符号后: 制表符攻击: !!!
用 16 个空格替换 \t 符号: 制表符攻击: !!!
sys.stdout
whether to forcibly flush the stream
是否强制刷新数据流?file
和 flush
是要挖坑的节奏,待我学完来填平,编码 Py011-1
# print()
name = "YorkFish"
print(name + name)
print(name, name) # 注意输出时有空格
print(name, name, sep=‘‘) # sep 设置为不空格
print() # 输出一次换行符
print(name, end=‘\n\n‘) # windows 下输出两次换行符
print(name, end=‘ --- ‘) # 意味着不换行
print(name)
YorkFishYorkFish
YorkFish YorkFish
YorkFishYorkFish
YorkFish --- YorkFish
# 例3 没学到,只好从官方文档中找了个例子
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
Py011-2
old
子字符串替换成 new
子字符串count
指定,则替换不超过 count
次# 例4
str4 = "All things in their being are good for something."
print(str4.replace(‘i‘, ‘I‘))
print(str4.replace(‘i‘, ‘I‘, 3))
All thIngs In theIr beIng are good for somethIng.
All thIngs In theIr being are good for something.
sep
作为分隔符字符串maxsplit
参数有设置,则仅分隔 maxsplit
个子字符串,返回切片后的子字符串拼接的列表# 例5
name5_1 = "Y.o.r.k.F.i.s.h"
name5_2 = "Y.o.r.k.F.i.s.h.."
print(name5_1.split(‘.‘))
print(name5_1.split(‘.‘,maxsplit=4))
print(name5_2.split(‘.‘)) # . 前的空字符算一个;. 后也算一个
[‘Y‘, ‘o‘, ‘r‘, ‘k‘, ‘F‘, ‘i‘, ‘s‘, ‘h‘]
[‘Y‘, ‘o‘, ‘r‘, ‘k‘, ‘F.i.s.h‘]
[‘Y‘, ‘o‘, ‘r‘, ‘k‘, ‘F‘, ‘i‘, ‘s‘, ‘h‘, ‘‘, ‘‘]
rsplit(sep=None, maxsplit=-1)
split(step=None, maxsplit=-1)
,不过是从右边开始分隔\n
分隔,返回一个包含各行作为元素的列表keepends
参数指定,则返回前 keepends
行# 例6.1
str6 = "Cease to struggle \nand you cease to live.\n(Thomas Carlyle)"
print(str6.splitlines()) # 拆分后去掉换行符
print(str6.splitlines(True)) # 若 keepends 为 True,保留换行符
[‘Cease to struggle ‘, ‘and you cease to live.‘, ‘(Thomas Carlyle)‘]
[‘Cease to struggle \n‘, ‘and you cease to live.\n‘, ‘(Thomas Carlyle)‘]
# 例6.2
print(‘‘.splitlines())
print(‘‘.split(‘\n‘)) # 注意两者的区别
print("love at first sight.\n".splitlines())
print("love at first sight.\n".split(‘\n‘))
[]
[‘‘]
[‘love at first sight.‘]
[‘love at first sight.‘, ‘‘]
prefix
开头,prefix
只是代号,可以是任意字符串start
、end
与之前样,可指定开头或指定区域# 例7
str7 = "The best is yet to come."
print (str7.startswith(‘The‘))
print (str7.startswith(‘is‘, 9))
print (str7.startswith(‘best‘, 4, 7)) # 和 endswith() 有些像,区域要大于指定字符长度
print (str7.startswith(‘best‘, 4, 8))
True
True
False
True
endswith(sub[, start[, end]])
见 第一弹# 例8
name8 = "YorkFish"
print(name8.swapcase())
yORKfISH
# 例9
str9 = "the great gatsby"
print(str9.title())
The Great Gatsby
width
的字符串width
大于字符串长度,前边用 0 填充# 例10
name10 = "YorkFish"
print(name10.zfill(5))
print(name10.zfill(20))
print(name10.zfill())
YorkFish
000000000000YorkFish
TypeError……zfill() takes exactly one argument (0 given)
报错是因为没有输入 width
参数。
别的也是:若方法的小括号中有参数,并且 1. 没有用中括号括起来,2. 没有默认值,都需要填上。
(元气大伤!)
[Python3] 011 字符串:给你们看看我的内置方法 第三弹
原文:https://www.cnblogs.com/yorkyu/p/10268679.html