Python字符串
字符串是一个元素以引号包围的,有序的,不可修改的序列。
字符串的定义: ‘’ 单引号或 “” 双引号
student="my name is hk hk hk" print(student.capitalize())#首字母大写 print(student.count("hk"))#统计hk有多少个 print(student.center(50,"_"))#居中打印50个字符,不够用—来填补。 print(student.endswith("hk"))#判断是否以hk 结尾。 print(student.find("n"))#查找n在字符串中是第几个 print(student[student.find("my"):9])#查找并显示以my开头,显示9个字符。
打印结果
My name is hk hk hk
3
_______________my name is hk hk hk________________
True
3
my name i
student1="he name is {name} and he is {year} old" print(student1.format(name="kkk",year=28)) print(student1.format_map({'name':"kkk","year":22})) print ('abc33'.isalnum()) print('abcA'.isalpha()) print('1A'.isdigit()) print('aaa222A'.isidentifier())
打印结果
he name is kkk and he is 28 old
he name is kkk and he is 22 old
True
True
False
True
print ('33A'.isspace()) print("My Name Is ".istitle()) print("My Name Is ".isprintable()) print("My Name Is ".istitle()) print("+".join(["A","B"])) print(student1.ljust(50,'*')) print(student1.rjust(50,'*'))
打印结果
False
True
True
True
A+B
my name is hk hk hk*******************************
*******************************my name is hk hk hk
print("aakezi".lower()) print("aakezi".upper()) print(' aaker.'.lstrip()) print(' aaker '.rstrip()) print(' aaker '.strip()) a=str.maketrans('abcde',"12345") print("abbedfg".translate(a)) print("kezi".replace("k","K",1)) print("kezi".rfind("e")) print("ke zi".split(' '))
打印结果
aakezi
AAKEZI
aaker.
aaker
aaker
12254fg
Kezi
1
['ke', 'zi']