跳转至

🔥AI副业赚钱星球

点击下面图片查看

郭震AI

字符串最常用的方法

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

大家在看

京ICP备20031037号-1 | AI之家 | AI资讯 | Python200 | 数据分析