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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| """ 这里写的是python的字符串常用的用法 """
str01 , str02 , str03 , str04 = 'javascript',"python","""css""",'''html''' str05 = str("markdown") print(str01,str02,str03,str04,str05) print(type(str01),type(str02),type(str03),type(str04),type(str05))
string = "hello world"
print(len(string)) print(string.count('o'))
print(string.capitalize()) print(string.upper()) print(string.lower()) print(string.swapcase())
print(chr(97)) print(ord('a'))
bs = b"ABCD" print(bs, type(bs)) print(bs[0])
string = "人生苦短 我用Goland" encode = string.encode("utf8") print(encode) decode = encode.decode("utf8") print(decode)
string = " java " print(string.strip()) print(string.lstrip()) print(string.rstrip())
string = "hello world" print(f"{string[0]},{string[1]}")
string = "hello world" print(f"{string.index('l')},{string.rindex('l')}")
string = "hello world" print(f"{string.replace('l','h')},{string.replace('l','h',1)}")
string = "hello world" print(f"{string.split('l')},{string.split('l',1)}") print(f"{string.split('l',2)}") print(f"{string.split('l',-1)}")
string = "hello world" print(f"{string[0:5]},{string[5:]}")
string = "hello world" for i in string: print(i, end=" ") print()
print("-".join(["hello", "world"])) print(",".join(("hello", "world"))) print("jack"+ str(30)+str(True))
path = "c:\\program_files\\works\\python\\set.py" print(path) path = r"c:\program_files\works\python\set.py" print(path)
not_words = ["tmd","sb"] content = input("评论:")
def filter_content(words,string): for w in words: string = string.replace(w,"*") return string
print(f"过滤前:{content}") result = filter_content(not_words,content) print(f"过滤后:{result}")
|