跳转至

字符串最常用的方法

本文总阅读量次 ,原创教程,严禁转载

字符串最常用的方法

1 strip

y = "
This is lazy\t\n
"
print(y.strip())
# Remove Whitespace: 'This is lazy'

2 lower upper

print("DrDre".lower())
# Lowercase: 'drdre'

print("attention".upper())
# Uppercase: 'ATTENTION'

3 startswith endswith

print("smartphone".startswith("smart"))
# Matches the string's prefix against the argument: True

print("smartphone".endswith("phone"))
# Matches the string's suffix against the argument: True

4 find replace

print("another".find("other"))
# Match index: 2

print("cheat".replace("ch", "m"))
# Replaces all occurrences of the first by the second argument: meat

5 join

print(','.join(["F", "B", "I"]))
# Glues together all elements in the list using the separator string: F,B,I

print(len("Rumpelstiltskin"))
# String length: 15

print("ear" in "earth")
# Contains: True