目录
字符串的遍历(3中方法)
str = "hello world"
print("\n方法1:")
for i in str:
print(i, end="~") # h~e~l~l~o~ ~w~o~r~l~d~
print("\n方法2: ")
for j in range(len(str)):
print(str[j], end="--") # h--e--l--l--o-- --w--o--r--l--d--
print("\n方法3:")
for k, v in enumerate(str): # k表示索引,v表示元素值
print(k, v)
运行结果:
方法1:
h~e~l~l~o~ ~w~o~r~l~d~
方法2:
h--e--l--l--o-- --w--o--r--l--d--
方法3:
0 h
1 e
2 l
3 l
4 o
5
6 w
7 o
8 r
9 l
10 d
字符串的拼接
一般使用 + 号
注意:只能是字符串和字符串进行拼接
str1 = "你好"
str2 = "世界"
str = str1 + str2
print(str)
# 结果:你好世界
# 只能字符串和字符串进行拼接,比如 字符串和整数拼接会报错
# str3 = "拼接数字报错"
# str4 = 123
# str = str3 + str4 # 报错
# print(str)
字符串切片操作
字符串反转
str = "你好"
print(str[::-1])
# 结果:好你
# 设置步长为2
str = "0123456789x"
print(str[0:10:2]) # 切片操作,包头不包尾
# 结果:02468字符 x 没有输出
字符串大小写转换
# 转大写
str = "Hello world"
print(str.upper()) # HELLO WORLD
# 转小写
print(str.lower()) # hello world
# 将大写字符转小写字母,将小写字母转大写字母
print(str.swapcase()) # hELLO WORLD
# 将字符串中的每个单词首字母转换为大写
str = "hello world"
print(str.title()) # Hello World
字符串填充
默认以空格填充
str = "你好"
print(str.center(40)) # 你好
print(str.center(40, '*')) # *******************你好*******************
# 向左填充 (原内容靠左)
str = "你好"
print(str.ljust(20, "%")) # 你好%%%%%%%%%%%%%%%%%%
# 向右填充
print(str.rjust(20, "%")) # %%%%%%%%%%%%%%%%%%你好
# zfill() 使用数字0填充字符串指定的长度 (原内容靠右)
print(str.zfill(20)) # 000000000000000000你好
strip() 去除字符串两边的指定字符
(默认去除的是空格)
str1 = " hello "
str2 = "*****hello*****"
print(str1.strip()) # hello
print(str2.strip('*')) # hello
# lstrip() 去除字符串左边指定的字符
print(str1.lstrip()) # hello (后面的空格没有去除)
print(str2.lstrip("*")) # hello*****
# rstrip() 去除右边
print(str2.rstrip("*")) # *****hello
字符串的分割 和 合并
(1) strip() 以指定的字符进行分割
str1 = "hello everyone nice too meet you"
str2 = "hello*everyone*nice*too*meet*you"
print(str1.split()) # ['hello', 'everyone', 'nice', 'too', 'meet', 'you']
print(str2.split("*")) # ['hello', 'everyone', 'nice', 'too', 'meet', 'you']
(2) striplines() 按照行进行分割
str = """床前明月光
疑是地上霜
举头望明月
低头思故乡
"""
print(str.splitlines()) # ['床前明月光', '疑是地上霜', '举头望明月', '低头思故乡']
join() 以指定的字符合并
s = "-"
list = ['hello', 'everyone', 'nice', 'too', 'meet', 'you']
print(s.join(list)) # hello-everyone-nice-too-meet-you
查找字符串
find(): 表示查找指定字符串在整个字符串第一次出现的位置,返回的是下标,若未找到返回-1
str = "123435464-abcuiejuAGYGIYgaaaaaai978721aaa3"
# find() 表示查找指定字符串在整个字符串第一次出现的位置,返回的是下标,若未找到返回-1
print(str.find("a")) # 10 查找的是整个范围
print(str.find("a", 12, 30)) # 25 在指定范围内查找
print(str.find("d")) # -1 查找的是整个范围 未找到返回-1
index() 和find()功能类似,若未找到直接报错
str = "123435464-abcuiejuAGYGIYgaaaaaai978721aaa3"
print(str.index("a")) # 10
print(str.index("a", 12, 30)) # 25 在指定范围内查找
# print(str.index("d")) # ValueError: substring not found
rfind() 表示查找指定字符串在整个字符串中 最后一次出现的位置,若找到则返回下标,未找到返回-1
str = "123435464-abcuiejuAGYGIYgaaaaaai978721aaa3"
print(str.rfind('a')) # 40
print(str.rfind('d')) # -1 未找到
print(str.rfind('a',10,20)) # 10 在指定范围内找
# 按照ASCII码值获取
str = "123435464-abcuiejuAGYGIYgaaaaaai978721aaa3"
# max() min()
print(max(str)) # u 获取的ASCII码值最大
print(min(str)) # - 获取的ASCII码值最小
字符串的替换
replace(原字符串,新字符串,替换次数)
str = """
一个人要成就自己的事业,不经过失败,不经过挫折,不花费较大的功夫是不可能成功的。
艰难困苦甚至失败打击固然会留下难以忍受的痛楚;但也正是在这种痛楚下才孕育出了一个个生命的奇迹。
我们都知道逆流而上的鱼儿,一路过险滩、趟急流、穿渔网、躲水鸟、过山涧、挤石隙、上高原、览冰川、虽结局并不圆满,但鱼儿可能并不后悔。
因为美在过程,人生亦如此,在追求梦想的过程中拼搏出的精彩与辉煌。"""
print(str.replace("不", "*"))
"""一个人要成就自己的事业,*经过失败,*经过挫折,*花费较大的功夫是*可能成功的。
艰难困苦甚至失败打击固然会留下难以忍受的痛楚;但也正是在这种痛楚下才孕育出了一个个生命的奇迹。
我们都知道逆流而上的鱼儿,一路过险滩、趟急流、穿渔网、躲水鸟、过山涧、挤石隙、上高原、览冰川、虽结局并*圆满,但鱼儿可能并*后悔。
因为美在过程,人生亦如此,在追求梦想的过程中拼搏出的精彩与辉煌。"""
print(str.replace("不", "*", 4))
"""一个人要成就自己的事业,*经过失败,*经过挫折,*花费较大的功夫是*可能成功的。
艰难困苦甚至失败打击固然会留下难以忍受的痛楚;但也正是在这种痛楚下才孕育出了一个个生命的奇迹。
我们都知道逆流而上的鱼儿,一路过险滩、趟急流、穿渔网、躲水鸟、过山涧、挤石隙、上高原、览冰川、虽结局并不圆满,但鱼儿可能并不后悔。
因为美在过程,人生亦如此,在追求梦想的过程中拼搏出的精彩与辉煌。"""
字符串的判断
1. isupper()
检测字符串的字母是否全是大写,返回布尔值。 若全部为大写返回True
str1 = "sjndwJHJHDIUKQHmkl"
str2 = "BIUHNIK"
print(str1.isupper()) # False
print(str2.isupper()) # True
2. islower()
检测字符串中的字母是否全是小写 若全部为小写返回True
str1 = "dnwiIHUHUASI"
str2 = "feirfitu"
print(str1.islower()) # False
print(str2.islower()) # True
3. isdigit()
检测字符串中的内容是否全部是数字,返回布尔值, 若全是数字返回True
str1 = "dweik2321"
str2 = "427846578"
print(str1.isdigit()) # False
print(str2.isdigit()) # True
4. istitle()
检测字符串中的单词的首字符是否是大写,返回布尔值, 若是返回True
str1 = "hello world"
str2 = "Hello World"
print(str1.istitle()) # False
print(str2.istitle()) # True
5. isalpha()
检测字符串中的内容是否全是字母或文字
str1 = "ndiuh你好7890"
str2 = "nisdfh你好sjio"
print(str1.isalpha()) # False
print(str2.isalpha()) # True
字符串前缀和后缀
(1)startswith() 判断字符串是否以指定字符开头,若是返回True
(2)endswith() 判断字符串是否以指定字符结尾,若是返回True
str1 = "HelloPython"
print(str1.startswith("Hello")) # True
print(str1.endswith("Python")) # True
字符串ASCAII值转换
#(1) chr()
print(chr(97)) # a
# (2)ord()
print(ord("a")) # 97