文本字符串处理也是常见的通用的处理方式,主要包括拼接、剪切、查找、匹配等
字符串通用操作
data = "I am a"
# 拼接
data = data + " student"
data = "I am a " "student"
data = ('I am a '
'student')
# 分隔
words = data.split(" ")
print(words)
# output: ["I", "am", "a", "student"]
# 索引
lan = "Python"
lan[0] # P
lan[-1] # n
lan[-2] # o
# 切片
lan[1:3] # "yt"
lan[:-3] # "Pyt"
lan[3:] # "hon"
lan[8:] # ""
lan[:10] # "Python"
# 计算长度
len(lan) # 6
# 拼接
“,”.join(words) # "I,am,a,student"
"/".join(["~", "log", "error.log"]) # "~/log/error.log"
以上是一些常见的字符串处理,更多更详细的内容可以参考下面的文档