版权声明:版权归本人,仅供大家参考 https://blog.csdn.net/Dream____Fly/article/details/82997271
字符串定义:
S="GHJBCAABSMNB"
字符串的相关基础知识:
1.字符串首字母大写:
S=“sbhsbhygbggahsj”
print(S.capitalize())
输出结果:Sbhsbhygbggahsj
2.计算字符串中指定字符的个数:
S=“sbhsbhygbggahsj”
print(S.count("s"))
输出结果:3
3.字符串大写变小写:
S="HGCSVSHNBDM"
print(S1.casefold())
输出结果:hgcsvshnbdm
4.字符串居中操作:
S="sbhsbhygbggahsj"
print(S.center(50,"+"))
输出结果:+++++++++++++++++sbhsbhygbggahsj++++++++++++++++++
5.字符串格式转化:
S="sbhsbhygbggahsj"
print(S.encode(encoding="utf-8"))
输出结果:b'sbhsbhygbggahsj'
6.字符串判断结尾:
S="sbhsbhygbggahsj"
print(S.endswith(".jpg"))
print(S.endswith("hsj"))
输出结果:False
True
7.查找指定字符在字符串中的位置:(只能查第一个)
S="sbhsbhygbggahsj"
print(S.find("b"))
输出结果:1
8.对字符串的格式操作:
name="{name} is {age}"
print(name)
print(name.format(name="Hao",age=22))
print(name.format_map({"name":"Hao","age":"22"}))
输出结果:{name} is {age}
Hao is 22
Hao is 22
9.对字符串操作:(该字符串操作,填入的参数是元组)
print("%s is " % "Hao")
print("%s is " % ("Hao"))
print("%s is %s" % ("Hao",22))
输出结果:Hao is
Hao is
Hao is 22
10.判断字符串是否由数字或字母组成:
S="22"
print(S.isalnum())
S1="h-hh"
print(S1.isalnum())
输出结果:True
False