Python中组合数据类型常用“字符串提取和分割与合并”strip()去除字符串两边的指定字符(默认去除的是空格)
案例学习:
str1="today is a nice day"str2="***today is a nice day***"# print(str1)# print(str1.strip())# print(str2)# print(str2.strip("*"))# lstrip() 只去除字符串左边的指定字符# print(str2.lstrip("*"))# rstrip() 只去除字符串右边的指定中字符# print(str2.rstrip("*"))# 字符串的分割和合并# split() 以指定字符对字符串进行分割(默认是空格)str3="this is a string example...."print(str3.split()) #['this','is','a','string','example....']# print(str3.split("i")) #['th','s a str','ng example....']# join() 合并字符串str4 = "-"tup = ("hello","every","body")print(tup)print(str4.join(tup))
输出结果如下:
today is a nice daytoday is a nice day***today is a nice day***today is a nice day******today is a nice day['this','is','a','string','example....']['th','s','s a str','ng example....']('hello','every','body')