>>>a = "asdjkljaskdjlkasd" >>>a 'asdjkljaskdjlkasd' >>>a.capitailize 'Asdjkljaskdjlkasd'
>>>a = “湖人总冠军” >>>a '湖人总冠军' >>>a.center(30,'*') '************湖人总冠军*************'
>>>b = "abcdrfghijklmn" >>>b 'abcdefghjiklmn' >>>b.rjust(20) ' abcdefghijklmn' >>> b.rjust(20,'-') '------abcdefghijklmn' >>>b.rjust(30,'*') '****************abcdefghijklmn'
>>>b = "abcdrfghijklmn" >>>b 'abcdefghjiklmn' >>>b.ljust(20) 'abcdefghijklmn ' >>> b.ljust(20,'-') 'abcdefghijklmn------' >>>b.ljust(30,'*') 'abcdefghijklmn****************'
1 >>> b = "aabbbbbccsdajkaklsdkkasdgn" >>> b 'aabbbbbccsdajkaklsdkkasdgn' >>> b.count(a) 0 >>> b.count('a') 5 >>> b.count('b') 5 >>> b.count('s') 3 >>> b.count('aa') 1 >>> b.count('jk') 1 2 >>> b = "abc,bcd,abc,asd,abc " >>> b 'abc,bcd,abc,asd,abc ' >>> b.count('abc') 3 >>>
>>> a = "abcdefhijklmn" >>> a 'abcdefhijklmn' >>> a.endswith('n') True >>> a.endswith('m') False
>>> a = "abcdefhijklmn" >>> a 'abcdefhijklmn' >>> a.startswith('m') False >>> a.startswith('a') True
>>> a = "abcdefhijklmn" >>> a 'abcdefhijklmn' >>> a.index('a') 0 >>> a.index('m') 11 >>> a.index('j') 8 >>> a.index('e') 4
>>> a = "asdhbknlqwieoohzlmhuhqwegjghsgjhasda" >>> a 'asdhbknlqwieoohzlmhuhqwegjghsgjhasda' >>> a.rindex('i') 10 >>> a.rindex('a') 35 >>> a.rindex('d') 34 >>> a.rindex('s') 33 >>> a.rindex('h') 31
>>> a = "asdhbknlqwieoohzlmhuhqwegjghsgjhasda" >>> a 'asdhbknlqwieoohzlmhuhqwegjghsgjhasda' >>> a.find('i') 10 >>> a.find('u') 19 >>> a.find('c') -1 >>> a.find('x') -1 >>> a.find('d') 2 >>>
>>> a = "asdhbknlqwieoohzlmhuhqwegjghsgjhasda" >>> a 'asdhbknlqwieoohzlmhuhqwegjghsgjhasda' >>> a.rfind('i') 10 >>> a.rfind('x') -1 >>> a.rfind('asdhbknlqwieoohzlmhuhqwegjghsgjhasda') 0 >>> a.rfind('v') -1 >>> a.rfind('s') 33
>>> s = "中国" >>> s '中国' >>> s.encode() b'\xe4\xb8\xad\xe5\x9b\xbd'
>>> "{} {}".format("hello", "world") 'hello world' >>> "{0} {1}".format("hello", "world") 'hello world' >>> "{1} {0} {1}".format("hello", "world") 'world hello world' >>>
(1.通过关键字.
print('{name}在{option}'.format(name="中国",option="维护和平"))
输出为:中国在维护和平
(2.通过位置.
print('name={} path={}'.format('11111', '/')
输出为:name=11111 path = /
print('{0}在{1}在{2}'.format('向某','写代码','房间'))
输出为:向某在写代码在房间
(3.填充和对齐
print('{:^30}'.format("111111")) # 居中 print('{:>30}'.format("222222")) # 右对齐 print('{:<30}'.format("333333")) # 左对齐 30:字段长度(最左到最右之间的长度)
输出结果为:
(4.精度掌控
print('{:.3f}'.format(3.1415926))
输出结果为:3.14(四舍五入且保留三位小数)
print('{:.5f}'.format(3.14))
输出结果为:3.14000(保留五位小数,不够的用零补齐)
print('{:b}'.format(100))二进制 print('{:o}'.format(100))八进制 print('{:d}'.format(100))十进制 print('{:x}'.format(100))十六进制
输出结果为:
>>> a = "asdakalsdkjcc" >>> a 'asdakalsdkjcc' >>> a.islower() True >>> b = "AsSDFsadojqwd" >>> b 'AsSDFsadojqwd' >>> b.islower() False
>>> A = "SKKJXHAKS" >>> A 'SKKJXHAKS' >>> A.isupper() True >>> B = "AASDasdakjasd" >>> B 'AASDasdakjasd' >>> B.isupper() False
>>> a = "Hello world" >>> a 'Hello world' >>> a.istitle() False >>> b = "Hello World" >>> b 'Hello World' >>> b.istitle() True
>>> a = "a s sdsad dfa " >>> a 'a s sdsad dfa ' >>> a.isspace() False >>> >>> b = " " >>> b ' ' >>> b.isspace() True
>>> a = "1adhhihwdhui2uhi97asdjioqwjei42343129048" >>> a '1adhhihwdhui2uhi97asdjioqwjei42343129048' >>> a.isdigit() False >>> b = "1289712739864" >>> b '1289712739864' >>> b.isdigit() True
>>> a = "12jjhuihihi" >>> a '12jjhuihihi' >>> a.isalnum() True
>>> a = "12jjhuihihi" >>> a '12jjhuihihi' >>> a.isalpha() False >>> b = "hjashduiihhbbqwuiudiewq" >>> b 'hjashduiihhbbqwuiudiewq' >>> b.isalpha() True
>>> a = "hello world word" >>> a 'hello world word' >>> a.title() 'Hello World Word'
>>> a = "HELLO WORLD WORD" >>> a 'HELLO WORLD WORD' >>> a.lower() 'hello world word'
>>> a = "hello world word" >>> a 'hello world word' >>> a.upper() 'HELLO WORLD WORD'
>>> a = "asdajkashdjhhjw" >>> a 'asdajkashdjhhjw' >>> a.split("asda,jkashd") ['asdajkashdjhhjw'] >>> a.split("asda") ['', 'jkashdjhhjw'] >>> a.split("asda,j,k,a,s,h,d") ['asdajkashdjhhjw'] >>> a.split("asda,j,k,") ['asdajkashdjhhjw'] >>> a.split("asda,j,k,")
>>> a = "sd" >>> a 'sd' >>> a.join(['cdf']) 'cdf' >>> a 'sd' >>> a.join(['cdf','a','bc']) 'cdfsdasdbc' >>> a 'sd'
>>> a = " asd " >>> a ' asd ' >>> a.strip() 'asd'
>>> a = " asd " >>> a ' asd ' >>> a.lstrip() 'asd '
>>> a = " asd " >>> a ' asd ' >>> a.rstrip() ' asd'
>>> a.replace('aaaaa','a') 'asd' >>> a.replace('aaaaa','asd') 'asd' >>> a.replace('asd','aaaaaaaaaaaa') 'aaaaaaaaaaaa' >>> a.replace('asd','aaaaaaaaaaaa')