1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
s1 = "hello world" print(len(s1)) # 11 print(s1.count('l')) # 3
s2 = "HELLO world" print(s2.upper()) # 'HELLO WORLD' print(s2.lower()) # 'hello world'
print(s2.title()) # Hello World
print(s2.capitalize()) # Hello world
print(s2.swapcase()) # 'hello WORLD'
s3 = '2+3' print(eval(s3)) s4 = '[1,2,3]' l4 = eval(s4) print(l4, type(l4)) # [1, 2, 3] <class 'list'>
print("hello".center(50)) print("hello".center(50, '-')) # 在50宽度居中显示,其他区域用'-'填充
print("hello".ljust(50, '-')) # 靠左 left print("hello".rjust(50, '-')) # 靠右 right
print("hello".zfill(50)) # 靠右,其他地方用0填充
s5 = "I like Python and Pycharm" print(s5.find('Py')) # 7 print(s5.find('Py22')) # -1
print(s5.rfind('Py')) # 18 print(s5.rfind('Py22')) # -1
print(" abc def ".strip()) # 'abc def', 默认将两边的空格去除 print("----韩 -- 信----".strip('-')) # '韩 -- 信' print("----韩 -- 信----".lstrip('-')) # '韩 -- 信----' print("----韩 -- 信----".rstrip('-')) # '----韩 -- 信'
print("HE".isupper()) # 是否为大写字母 print('h'.islower()) # 是否为小写字母
print('33'.isdigit()) # 是否为数字 print('a'.isalpha()) # 是否为字母 print('abcABC123'.isalnum()) # 是否为数字或字母
|