Python_字符串
1.字符串的写法
#字符串的写法
# 写法一:
a='LiMing'
print(a)
print(type(a))
# 写法二:
b='''LiMing'''
print(b)
print(type(b))
# 写法三:
c="LiMing"
print(c)
print(type(c))
# 写法四:
d="""LiMing"""
print(d)
print(type(d))
# 写法五:
e='I\'m LiMing'
print(e)
print(type(e))
2.字符串的输出
# 字符串的输出
print('hello world')
# 需求:我的名字是LiMing
name='LiMing'
print('我的名字是%s' %name)
print(f'我的名字是{name}')
3.字符串的输入
# 字符串的输入
password=input('请输入您的密码:')
print(f'您输入的密码是{password}')
print(type(password)) #字符型类型
4.下标(索引)
(1)作用:通过下标快速找到对应的数据
(2)注意:下标从0开始
# 下标
#需求:得到字符b和字符g
str='abcdefg'
# print(str) 输出全部值
print(str[1]) #b
print(str[6]) #g
5.切片
(1)定义:对操作的对象截取其中一部分内容。字符串、列表、元组都支持切片操作
(2)语法:序列名[开始位置下标:结束位置下标:步长]
(3)注意:
A:不包含结束位置下标对应的数据,正负数均可
B:步长是选取间隔,正负数均可,默认步长为1
C:如果选取方向(下标开始到结束的方向)和步长的方向冲突,则无法选取数据
# 切片
str1='abcdef'
# 需求:得到整个字符串数据
print(str)
str2='0123456789'
# 需求:得到23456
print(str2[2:7:1])
# 需求:得到02468
print(str2[0:9:2])
# 需求:得到23456
print(str2[2:7]) #默认步长为1
# 需求:得到0123
print(str2[:4]) #默认下标从0开始
# 需求:得到23456789
print(str2[2:]) #如果不写结束位置,则默认选取到最后
# 需求:得到0123456789
print(str2[:]) #不写开始和结束,默认选取所有
#负数测试
#倒序排列
print(str2[::-1])
#需求:得到45678
print(str2[-6:-1]) #-1表示最后一个数据,以此向前类推
# #需求:得到
# print(str2[-6:-1:-1]) #方向冲突
#需求:得到98765
print(str2[-1:-6:-1]) #方向一致
6.字符串常用操作方法
- 查找:查找子串在字符串中的位置或者出现的次数
(1)find():检测某个子串是否包含在这个字符串中,如果在则返回这个子串开始位置的下标,否则返回-1
语法:字符串序列.find(子串,开始位置下标,结束位置下标)
注意:开始位置和结束位置下标可以省略,表示在整个字符串序列中查找
(2)index():检测某个子串是否包含在这个字符串中,如果在则返回这个子串开始位置的下标,否则报错
语法:字符串序列.index(子串,开始位置下标,结束位置下标)
注意:开始位置和结束位置下标可以省略,表示在整个字符串序列中查找
(3)count():返回某个子串在字符串中出现的次数
语法:字符串序列.count(子串,开始位置下标,结束位置下标)
注意:开始位置和结束位置下标可以省略,表示在整个字符串序列中查找
(4)rfind():和find()功能相同,但查找方向从右侧开始
(5)rindex():和index()功能相同,但查找方向从右侧开始
# 字符串常用操作方法
# 1.find()函数
str='hello world and hello school and hello'
print(str.find('and')) #12
print(str.find('and',14,36)) #29
print(str.find('ands')) #-1 不存在ands,找不到
# 2.index()函数
print(str.index('and')) #12
print(str.index('and',14,36)) #29
print(str.index('ands')) #如果index查找的子串不存在,则报错
# 3.count()函数
print(str.count('and',)) #2
print(str.count('and',16,36)) #1
print(str.count('ands')) #0
# 4.rfind() 函数
print(str.rfind('and')) #29
print(str.rfind('ands')) #-1
# 5.rindex() 函数
print(str.rindex('and')) #29
print(str.rindex('ands')) #报错
- 修改:通过调用函数的形式修改字符串中的数据
(1)replace():替换(有返回值,返回值为修改后的字符串)
语法:字符串序列.replace(旧子串,新子串,替换的次数)
注意:替换次数如果超出子串出现次数,则替换次数为该子串出现次数;不报错
# 修改
str='hello world and hello school and hello' #不可变类型
# 1.replace()函数
new_str=str.replace('and','he')
print(new_str)
new_str=str.replace('and','he',1)
print(new_str)
new_str=str.replace('and','he',10)
print(new_str)
(2)split():按照指定字符分割字符串,返回一个列表,会丢失分割符号
语法:字符串序列.split(分割字符,num)
num:表示的是分割字符出现的次数,即将来返回数据个数为num+1个
# 修改
str='hello world and hello school and hello ' #不可变类型
# 2.split() 分割函数
list1=str.split('and')
print(list1)
list2=str.split('and',1)
print(list2)
(3)join():合并列表中的字符串为一个大的字符串
语法:字符或者子串.join(多字符串组成的序列)
# 3.join() 函数
list=['aa','bb','cc']
new_str='///'.join(list)
print(new_str) #aa///bb///cc
(4)capitalize():字符串首字母大写
(5)title():字符串中每个单词首字母大写
(6)upper():小写转大写
(7)lower():大写转小写
# 修改
str='hello World and hello School and hello' #不可变类型
# 4.capitalize()函数;字符串首字母大写
new_str1=str.capitalize()
print(new_str1)
# 5.title()函数:字符串中每个单词首字母大写
new_str2=str.title()
print(new_str2)
# 6.upper():小写转大写
new_str3=str.upper()
print(new_str3)
# 7.lower()函数:大写转小写
new_str4=str.lower()
print(new_str4)
(8)lstrip():删除字符串左侧空白
(9)rstrip():删除字符串右侧空白
(10)strip():删除字符串两侧空白
# 修改
str=' hello World and hello School and hello '
# 8.lstrip():删除左侧空白
new_str1=str.lstrip()
print(new_str1)
# 9.rstrip():删除右侧空白
new_str2=str.rstrip()
print(new_str2)
# 10.strip():删除两侧空白
new_str3=str.strip()
print(new_str3)
(11)ljust():字符串左侧对齐
语法:字符串序列.ljust(长度,填充字符)
(12)rjust():字符串右侧对齐
语法:字符串序列.rjust(长度,填充字符)
(13)center():字符串居中对齐
语法:字符串序列.center(长度,填充字符)
- 判断
(1)start():检查字符串是否以指定子串开头,是则返回True,否则返回False。如果没有设置开始和结束位置下标,则在指定范围内检查
语法:字符串序列.startswith(子串,开始位置下标,结束位置下标)
(2)start():检查字符串是否以指定子串结尾,是则返回True,否则返回False。如果没有设置开始和结束位置下标,则在指定范围内检查
语法:字符串序列.endswith(子串,开始位置下标,结束位置下标)
# 判断
#1. startswith()函数
str='hello World and hello School and hello'
print(str.startswith('hello')) #True
print(str.startswith('hel')) #True
print(str.startswith('hes')) #False
# 2.endswith()函数
str='hello World and hello School and'
print(str.endswith('and')) #True
print(str.endswith('d')) #True
print(str.endswith('a')) #False
(3)isalpha():如果字符串非空并且所有字符都是字母,则返回True,否则返回False
(4)isdigit():如果字符串中只包含数字则返回True,否则返回False
(5)isalnum():如果字符串非空并且所有字符都是字母或者数字,则返回True,否则返回False
(6)isspace():如果字符串中只包含空格,则返回True,否则返回False
str1='abcd'
str2='1234'
str3='av13'
str4=' '
# 3.isalpha():字母
print(str1.isalpha()) #True
print(str3.isalpha()) #False
# 4.isdigit():数字
print(str2.isdigit()) #True
print(str3.isdigit()) #False
# 5.isalnum():数字或字母组合
print(str3.isalnum()) #True
print(str1.isalnum()) #True
print(str2.isalnum()) #True
#6.isspace():空格
print(str4.isspace()) #True