标签:对齐 lines color encode 找不到 参考 sla 简单的 JD
1 name = ‘Jack‘ 2 age = 22 3 course = [‘web‘,‘Java‘,‘mysql‘,‘linux‘] 4 greads = [80,89,96,72] 5 print(‘使用str()函数前:‘) 6 print(type(name)) 7 print(type(age)) 8 print(type(course)) 9 print(type(greads)) 10 print(‘使用str()函数后:‘) 11 print(type(str(name))) 12 print(type(str(age))) 13 print(type(str(course))) 14 print(type(str(greads)))
运行结果为:
使用str()函数前: <class ‘str‘> <class ‘int‘> <class ‘list‘> <class ‘list‘> 使用str()函数后: <class ‘str‘> <class ‘str‘> <class ‘str‘> <class ‘str‘>
1 s=‘asdfg hjkl‘ 2 print(s.capitalize())
运行结果为:
Asdfg hjkl
1 s1 = ‘asdfghjkl‘ 2 s2 = ‘ZXCVBNM‘ 3 print(s1.swapcase()) 4 print(s2.swapcase())
运行结果为:
ASDFGHJKL
zxcvbnm
1 s = ‘COME ON‘ 2 print(s.center(20))#长度为20,以空格填充 3 print(s.center(20,‘*‘))#长度为20,以‘*’填充
运行结果为:
COME ON
******COME ON*******
1 s = ‘aghyghaya‘ 2 print(s.count(‘a‘))#查找所有字符 3 print(s.count(‘a‘,3))#从第3个开始查找 4 print(s.count(‘a‘,3,9))#从第3个到第9-1个 5 print(s.count(‘gh‘,1,9))#查找‘gh’
运行及如果为:
3 2 2 2
1 s1 = ‘你好,世界!‘ 2 s2 = s1.encode(‘UTF-8‘,‘strict‘) 3 print(s2) 4 print(s2.decode())
运行结果为:
b‘\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81‘ 你好,世界!
1 s = ‘asdfghjklpoiuytrewq‘ 2 print(s.endswith(‘ewq‘)) 3 print(s.endswith(‘jkl‘,0,9)) 4 print(s.endswith(‘sdf‘))
运行结果为:
True
True
False
1 s = ‘asd\tfgh\tjkl‘ 2 print(s.expandtabs()) 3 print(s.expandtabs(2))
运行结果:
asd fgh jkl
asd fgh jkl
1 s = ‘asdfghasdfcvgfdsa‘ 2 print(s.find(‘as‘)) 3 print(s.find(‘as‘,2)) 4 print(s.find(‘as‘,2,5)) 5 print(s.find(‘po‘)) 6 7 print(‘r‘.center(10,"*")) 8 print(s.rfind(‘as‘)) 9 print(s.rfind(‘as‘,2)) 10 print(s.rfind(‘as‘,2,5)) 11 print(s.rfind(‘po‘))
运行结果为:
0
6
-1
-1
****r*****
6
6
-1
-1
1 s = ‘{0},{1} {2}‘ 2 print(s.format(‘Hello‘,‘my‘,‘friends.‘)) 3 s = ‘{2},{1} {2}‘ 4 print(s.format(‘Hello‘,‘my‘,‘friends.‘))
运行结果为:
Hello,my friends.
friends.,my friends.
1 s = ‘asdfghasdfcvgfdsa‘ 2 print(s.index(‘as‘)) 3 print(s.index(‘as‘,2)) 4 print(s.index(‘as‘,2,5)) #报错 5 print(s.index(‘po‘)) #报错
运行结果为:
0 Traceback (most recent call last): 6 File "E:/.../str-index.py", line 4, in <module> print(s.index(‘as‘,2,5)) #报错 ValueError: substring not found
1 s = ‘Hello, world!‘ 2 print(‘-‘.join(s,)) 3 print(‘*‘.join(s,))
运行结果为:
H-e-l-l-o-,- -w-o-r-l-d-!
H*e*l*l*o*,* *w*o*r*l*d*!
1 s = ‘asdfghjkl‘ 2 print(s.ljust(20,‘*‘)) 3 print(s.rjust(20,‘*‘))
运行结果为:
asdfghjkl*********** ***********asdfghjkl
1 s1 = ‘ asdfghjkl ‘ 2 s2 = ‘****asdfghjkl****‘ 3 print(s1.strip()) 4 print(s2.strip(‘*‘)) 5 print(s2.rstrip(‘*‘)) 6 print(s2.lstrip(‘*‘))
运行结果为:
asdfghjkl asdfghjkl ****asdfghjkl asdfghjkl****
1 s = ‘ASDFghJKL‘ 2 print(s.lower())
运行结果为:
asdfghjkl
1 s1 = "asdfghjkl" 2 s2 = "123456789" 3 s = str.maketrans(s1,s2) 4 str = ‘qawsedrftgyhujikolp‘ 5 print(str.translate(s))
运行结果为:
q1w2e3r4t5y6u7i8o9p
1 s = ‘asdfghjkl‘ 2 print(s.partition(‘fgh‘))
运行结果:
(‘asd‘, ‘fgh‘, ‘jkl‘)
1 s = ‘asdfghjkl‘ 2 print(s.replace(‘df‘,‘@#$%^&*‘))
运行结果为:
as@#$%^&*ghjkl
1 s = ‘asdfghjkl‘ 2 print(s.split(‘gh‘)) 3 print(s.rsplit(‘gh‘))
运行结果为:
[‘asdf‘, ‘jkl‘] [‘asdf‘, ‘jkl‘]
1 s = ‘asdghjjki\nuytfghj\njjhfkdsj\nhfajhfjdskhfjdskhf‘ 2 print(s.splitlines())
运行结果为:
[‘asdghjjki‘, ‘uytfghj‘, ‘jjhfkdsj‘, ‘hfajhfjdskhfjdskhf‘]
1 str = ‘asdfghjkl‘ 2 print(str.startswith(‘asd‘)) 3 print(str.startswith(‘dfg‘,2)) 4 print(str.startswith(‘dfg‘,2,7)) 5 print(str.startswith(‘fds‘))
运行结果为:
True
True
True
False
1 s1 = ‘ asdfghjkl ‘ 2 s2 = ‘****asdfghjkl****‘ 3 print(s1.strip()) 4 print(s2.strip(‘*‘)) 5 print(s2.rstrip(‘*‘)) 6 print(s2.lstrip(‘*‘))
运行结果为:
asdfghjkl asdfghjkl ****asdfghjkl asdfghjkl****
1 s = ‘asdfgHJKL‘ 2 print(s.swapcase())
运行结果为:
ASDFGhjkl
1 s = ‘my love will go on‘ 2 print(s.title())
运行结果为:
My Love Will Go On
1 s = ‘qwe rfg hbn jk‘ 2 print(s.upper())
运行结果为:
QWE RFG HBN JK
1 s = ‘asdf‘ 2 print(s.zfill(8))
运行结果为:
0000asdf
本文参考链接http://www.runoob.com/python/python-strings.html
标签:对齐 lines color encode 找不到 参考 sla 简单的 JD
原文地址:https://www.cnblogs.com/lgqrlchinese/p/8987947.html