暂无 |

字符串函数

Python包含许多有用的内置函数和方法来完成常见任务。
join一个字符串列表和另一个字符串作为分隔符。
replace-替换另一个字符串中的一个子字符串。
startswith 和 endswith-分别确定字符串的开始和结束是否有子字符串。
若要更改字符串的大小写,可以使用lower 和 upper.。
方法split 与join相反,将带有某个分隔符的字符串转换成列表。

例如:

print(", ".join(["spam", "eggs", "ham"]))
# 输出结果 "spam, eggs, ham"

print("Hello ME".replace("ME", "world"))
# 输出结果s "Hello world"

print("This is a sentence.".startswith("This"))
# 输出结果 "True"

print("This is a sentence.".endswith("sentence."))
# 输出结果 "True"

print("This is a sentence.".upper())
# 输出结果 "THIS IS A SENTENCE."

print("AN ALL CAPS SENTENCE".lower())
# 输出结果 "an all caps sentence"

print("spam, eggs, ham".split(", "))
# 输出结果"['spam', 'eggs', 'ham']"

0

发表评论

    评价:
    验证码: 点击我更换图片
    最新评论