Python基础数据类型之字符串(二)
一、字符串的常规操作
字符串的操作一般不会对原字符产生影响,一般是返回一个新的字符串。
二、字符串的大小写转换
1、首字母大写
# 字符串的大小转换
m = "python"
m1 = m.capitalize()
print(m1)
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/04_字符串的常规操作.py
Python
Process finished with exit code 0
2、 每个单词首字母大写
s = "I believe you can succeed"
s1 = s.title()
print(s1)
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/04_字符串的常规操作.py
I Believe You Can Succeed
Process finished with exit code 0
3、大写转化为小写
s = "I LIKE YOU"
s1 = s.lower()
print(s1)
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/04_字符串的常规操作.py
i like you
Process finished with exit code 0
4、所有字母变成大写字母
s = "I believe you can succeed"
s1 = s.upper() # 把所有字母变成大写字母
print(s1)
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/04_字符串的常规操作.py
I BELIEVE YOU CAN SUCCEED
Process finished with exit code 0
二、验证码忽略大小写
# 如何忽略大小写进行判断
verify_code = "alYq1"
user_input =input(f"请输入验证码({verify_code}):")
if verify_code.upper() == user_input.upper():
print("验证码正确")
else:
print(