原始字符串
\ :转义字符
d = 'E:\\python_Projects\\day01'
d = r'E:\python_Projects\day01'
print(d)
结果:E:\python_Projects\day01
只有windows能识别反斜杠\ E:\python_Projects\day01
所有系统都兼容正斜杠/ E:/python_Projects/day01
带占位符的字符串
d = 'E:\\python_Projects\\day01'
print(f'文件路径{d}')
案例:
# 输出出现次数最多的元数
nums = [10, 10, 1, 1, 20, 20, 10, 1]
# 假设第一个元素次数最多
items, max_count = [nums[0]], nums.count(nums[0])
for num in nums[1:]:
current_count = nums.count(num)
if current_count > max_count:
max_count = current_count
items.clear()
items.append(num)
elif current_count == max_count:
if num not in items:
items.append(num)
for item in items:
print(item)
字符串的运算
1、拼接和重复运算 + 、*
s1 = 'hello' + ' ' + 'world'
print(s1) # hello world
s2 = '!' * 3
print(s2) # !!!
2、比较运算 ==
#比较运算
a == b 比较字符串内容
3、成员运算 in 、not in
a = 'hello word'
#判断字符是否存在
print('or' in a)
4、获取字符串长度 len()
s1 = 'hello, world'
print(len(s1)) # 12
5、切片和索引 sum[1:] 、sum[0]
运算符是[i:j:k],其中i是开始索引,索引对应的字符可以取到;j是结束索引,索引对应的字符不能取到;k是步长,默认值为1,当k > 0
时表示正向切片(从前向后获取字符),当k < 0
时表示负向切片(从后向前获取字符)
s1 = 'abc123456'
print(s1[2:]) # c123456
6、字符串的遍历 for i in sum
a = 'hello word'
# 遍历字符串
for i in range(len(a)):
print(i)
for i in a:
print(i)
案例:屏幕轮播文字
# 跑马灯效果
#执行命令:python demo.py
#使用字符串切片和索引完成,把第一个字符放在末尾
content = '拼搏到无能为力 '
import time
import os
while True:
os.system('cls')
print(content)
time.sleep(0.5)
content = content[1:] + content[0]
字符串的方法
大小写相关操作
s1 = 'hello, world!'
# 字符串首字母大写
print(s1.capitalize()) # Hello, world!
# 每个单词首字母大写
print(s1.title()) # Hello, World!
# 大写
print(s1.upper()) # HELLO, WORLD!
s2 = 'GOODBYE'
# 小写
print(s2.lower()) # goodbye
在一个字符串中查找另外一个字符串
使用字符串的find
或index
方法,index方法找不到会报错,finde方法找不到返回-1,所以推荐使用find方法。rfind和rindex是逆向查找。
str = 'hello apple,I love apple'
str.count("h") #查找h的个数
print(str.find('apple')) # 输出第一次出现的索引
print(str.find('apple', 7)) # 从索引7开始找
print(str.rfind('apple')) # 从字符串尾部开始查找
print(str.index('apple')) # 输出第一次出现的索引
print(str.index('apple', 7)) # 从索引7开始找
print(str.rindex('apple')) # 从字符串尾部开始查找
字符格式化
使用center,ljust,rjust使字符串居中,左对齐,右对齐。
s = 'hello word'
# 以80字符的总宽度将字符串居中,使用=填充,默认用空格填充
print(s.center(80, '='))
# 左对齐
print(s.ljust(80, '*'))
# 右对齐
print(s.rjust(80, '*'))
#左边补零,达到13个字符
print(s.zfill(13)) # 000hello word
#python3.6之后
a = 12
b = 5
print(f'{a} + {b} = {a + b}') # f 就是formart的缩写
print(f'{a} * {b} = {a * b:.2f}')
如果需要进一步控制格式化语法中变量值的形式,可以参照下面的表格来进行字符串格式化操作。
修剪操作
s = ' hello everyone '
print(s.strip()) # 去掉字符串两端空格
print(s.lstrip()) # 去掉左边空格
print(s.rstrip()) # 去掉右边空格
#去除左侧和右侧匹配的字符“h”“e”"l""o"和" "
s = "hello word_he"
print(s.strip("hello ")) #word_
字符串替换
#字符替换
s = 'hello everyone'
print(s.replace('hello','haha')) # haha everyone
print(s.replace('h','a')) # aello everyone
#制定一个替换规则
x = "12321"
table = str.maketrans("123", "abc","1") #替换规则,第三个参数表示忽略这个元素的替换,可有可无
print(x.translate(table)) #abcba
拆分/合并操作
x = 'you go your way, I will go mine'
print(x.split()) #返回列表,使用列表的索引可以获取
#默认使用空格拆分 ['you', 'go', 'your', 'way,', 'I', 'will', 'go', 'mine']
print(x.split(','))
#使用逗号拆分 ['you go your way', ' I will go mine']
b = "www.blibli.com"
print(b.split(".", 1))
#后面的参数表示拆分几次['www', 'blibli.com']
rsplit:从右往左切分
lsplit:从左往右切分
#用换行符拆分
b = "www\nblibli\rcom\r\n"
print(b.splitlines()) #['www', 'blibli', 'com']
print(b.splitlines(True)) #拆分之后保留换行符 ['www\n', 'blibli\r', 'com\r\n']
content = [
'请不要相信我的美丽',
'更不要相信我的爱情',
'因为涂着油彩的面孔下'
]
print('*'.join(content))
#使用*进行连接:请不要相信我的美丽*更不要相信我的爱情*因为涂着油彩的面孔下
案例:是否为回文数(正序逆序结果一样)
x = "12321"
print("是回文数"if x == x[::-1]else "不是回文数") #三元运算