1.strip()去掉字符串左右两边的空白符(空格,、\t)
s = " 你好, 我 叫 周杰伦 ! "
s1 = s.strip()
print(s1)
![]()
用处:模拟用户登录
username = input("请输入用户名:").strip()
password = input("请输入密码:").strip() #去掉空格
if username == "admin":
if password == "123456":
print("登录成功")
else:
print("登录失败")
else:
print("登录失败")
2.字符串替换replace(old,new)
s = "你好,我叫陶喆"
s1 = s.replace("陶喆","周杰伦")
print(s1)
d = "i am lonely"
d1 = d.replace(" ","")#去掉所有的空格
print(d1)
![]()
3.字符串切割,split(用什么来切割),用谁来切,就会损失掉谁。
s = "python_java_c_c#_django_javascript_linux_vue_react"
s1 = s.split("_") #切割之后的结果会放在列表当中 list[]
print(s1)
![]()
4. 查找和判断
s = "你好,我叫周润发"
ret = s.find("周润发")
res = s.find("周润发123")
as = s.index("周润发") #index 没有就报错
print(ret)
print(res) #返回-1,表示没有该字符按串出现

s = "你好,我叫周润发"
print("周润发" in s)
print("李白" in s)

len()字符串的长度,字符串中有几个字符

887

被折叠的 条评论
为什么被折叠?



