str = 'zi_fu_chuan'
str2 = ' zi_fu_chuan'
#字符串长度
lenth = len(str)
print(lenth) #11
#分割字符串
splitstr = str.split('_')
print(splitstr) #['zi', 'fu', 'chuan']
#合并字符串
joinstr = ''.join(splitstr)
print(joinstr) #zifuchuan
#大小写转换
#标题化字符串(所有单词首字母大写)
titlestr = ''.join(x.title() for x in splitstr)
print(titlestr) #ZiFuChuan
titlestr2 = splitstr[0] + ''.join(x.title() for x in splitstr[1:])
print(titlestr2) #ziFuChuan
#小写
lowerstr = joinstr.lower()
#大写
upperstr = joinstr.upper()
#删除字符串左右两个的空格和特殊字符
stripstr = str2.strip()
print(stripstr) #zi_fu_chuan
#向指定字符串的左右侧填充指定字符,使字符串居中
centerstr = titlestr.center(35,'-')
print(centerstr) #-------------ZiFuChuan-------------
#检索字符串是否以指定字符串开头
print(titlestr.startswith('a')) #False
print(titlestr.endswith('an')) #True
python字符串处理
最新推荐文章于 2024-06-06 00:30:00 发布