Python🐍

python - string(2)

2020-10-11  本文已影响0人  Pingouin

早期计算编码由英语国家主导,原始ASCII编码只有128个,不够编码欧洲语言,更不用说中文了:D
python3的默认编码是utf-8. ASCII是utf-8的子集。
万维网上大约一半的页面都以UTF-8编码。

'abc'<'cde' # different at index 0, 'a'< 'c' True
'abc'<'abd' # different at index 2, 'c'< 'd' True
'abc'<'abcd' # 'abc' equal up to 'd' but shorter than 'abcd' True
''<'a' # the empty string's length is 0, always smaller True

String Method

hello_word = 'hello.  world'
hello_word.find('l') #2
hello_word.find('a') #-1
#注意找不到返回的是-1而不是0是因为0可以表示第一个index
#find('t',1,6) searches for 't' in the index range 1–6
hello_word = 'hello.  world'
hello_word.find('l',hello_word.find('l')+2)

输出格式

print("Pi is {:8.4f}".format(math.pi)) # 8 width 4位小数
print("{:8.2%}".format(2/3)) # 百分数形式 8 width 2位小数
print('{0} is {2} and {0} is also {1}'.format('Bill',25,'tall')) 
#Bill is tall and Bill is also 25

control and string

string.punctuation:'!"# $%&'()*+,-./:;<=>?@[\]ˆ_`{} ̃'|
string.digits: '0123456789'
string.ascii_lowercase: 'abcdefghijklmnopqrstuvwxyz'
string.whitespace: '\t\n\x0b\x0c\r '

上一篇 下一篇

猜你喜欢

热点阅读