字符串的增删改查操作

字符串的增删改查操作
增:
a ="alex"
b ="lhf"
c =a+b
print(c)

:strip
a=" +++haohaohoa___ "
b=a.strip().strip("+")
print(b)
c=a.lstrip()
print(c)
d=a.rstrip()
print(d)


改:replace,替换子字符串,lower,将大写改为小写,upper,将小写改为大写。
a ="alex"
b =a.replace("ex","oo")
c =a.lower()
d =a.upper()
print(b,c,d)


查:index,从左侧开始找对应元素的下标,取第一个,找不到则报错。find....取第一个,找不到返回特定值。count,查找特定字符在字符串中出现的次数
title:将首字母大写
a ="alex"
print(a.index("a"))
print(a.find("b"))
print(a.find("a"))
print(a.find("al"))
print(a.rfind("x"))
print(a.count("a"))
print(a.title())

猜你喜欢

转载自www.cnblogs.com/yyk520/p/11431777.html