python学习(day4)数据——字符串
1.认识字符串
1.1什么是字符串
字符串是 Python 中最常⽤的数据类型。我们⼀般使⽤引号来创建字符串。创建字符串很简单,只要为变量分配⼀个值即可
示例:
a = 'hello world'
b='nihao'
print(a)
print(type(a))
print(b)
print(type(b))
运行结果:
== <class ‘str’> , 即数据类型为str(字符串)==
1.2字符串特征
1:一对引号字符串
name1 = 'Tom'
name2 = "rose"
#这两种形式都是可以的
2:双引号字符串
特征:支持换行使用
b = '''hello
world'''
print(b)
#或者
b="""hello
world"""
print(b)
注意事项:如何创建I’m Tom ?
str = "I'm tom"#可以
str1 = 'I\'m tom'#\为转义字符
str2 = '''I'm tom'''#可以
str3 = """I'm tom"""#可以
print(str)
print(str1)
print(str2)
print(str3)
运行结果:
1.3字符串输出
name1='Tom'
print('你的名字是:%s' % name1)
print(f'你的名字是:{name1}')#推荐使用这个
运行结果:
2.下标
下标的作用:“下标” ⼜叫 “索引” ,就是编号。⽐如⽕⻋座位号,座位号的作⽤:按照编号快速找到对应的座位。同理,下标的作⽤即是通过下标快速找到对应的数据
示例:
str1='hello world'
print(str1[0])
print(str1[1])
print(str1[2])
注意事项:下标的索引值是从0开始的,即0对应的是第一个字母,为此在使用时多加注意
3.切片
**作用:**切⽚是指对操作的对象截取其中⼀部分的操作。字符串、列表、元组都⽀持切⽚操作。
3.1切片的语法
序列[开始位置下标:结束位置下标:步⻓]
-
注意事项
-
不包含结束位置下标对应的数据, 正负整数均可
-
步⻓是选取间隔,正负整数均可,默认步⻓为1
示例:`
name = "abcdefg"
print(name[1:5:1])#bcde
print(name[1:5])#bcde
print(name[:5])#bcde
print(name[1:])#bcdefg
print(name[:])#abcdefg
print(name[-4:-1])#def
print(name[::-1])#gfedcba
运行结果:
4.字符串的操作方法
字符串的常⽤操作⽅法有查找、修改和判断三⼤类
4.1查找
查找:即是查找⼦串在字符串中的位置或出现的次数
4.1.1查找的函数:
语法:字符串序列.函数(⼦串, 开始位置下标, 结束位置下标)(注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找)
函数列表:
函数 | 解释说明 |
---|---|
find | 检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返回-1 |
index | 检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则报异常 |
rfind | 作用和find是相同的,只是默认从右侧查找,返回的下标还是从左侧开始得下标 |
rindex | 作用和index是相同的,只是默认从右侧查找,返回的下标还是从左侧开始得下标 |
count | 返回某个⼦串在字符串中出现的次数 |
示例:
str1='hello world'
print(str1.find('h'))#查找的是字符串中字符‘l’的位置,查找到后就停止查找
print(str1.find('p'))#此时返回-1,未查找到
print(str1.index('l'))#查找‘l’的位置,返回下标
#print(str1.index('p'))#未查找到报错ValueError: substring not found
print(str1.rfind('l'))
print(str1.rindex('r'))
print(str1.count('ll'))#查找‘l’在字符串中出现的次数,没有返回
运行结果:
带有开始位置下标的和结束位置下标的请自行尝试
4.2修改
4.2.1修改的函数
1.replace函数(替换)
作用:所谓修改字符串,指的就是通过函数的形式修改字符串中的数据
replace语法:字符串序列.函数(旧⼦串, 新⼦串, 替换次数)
示例:
str2='Happiness is a way station between too much and too little.'
count_too=str2.count('too')#统计too的次数
print(count_too)
new_str2=str2.replace('too','ooo',count_too)#用ooo替换too次数为count_too次
print(new_str2)
运行结果:
注意事项:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型
2.split函数(分割)
作用:按照指定字符分割字符串
split语法:字符串序列.split(分割字符, num)
示例:
str2='Happiness is a way station between too much and too little.'
count_too=str2.count('too')#统计too的次数
print(count_too)
new_str2=str2.split('too',count_too)
print(new_str2)
运行结果:
-
注意事项
- num表示的是分割字符出现的次数,即将来返回数据个数为num+1个
- 返回的数据类型是列表(<class ‘list’>)
3.join函数(合并)
作用:⽤⼀个字符或⼦串合并字符串,即是将多个字符串合并为⼀个新的字符串
join语法:字符或⼦串.join(多字符串组成的序列)
示例:
lis11=['aa','bb','cc','dd']
new_str='+'.join(lis11)
print(new_str)
运行结果:
4.capitalize():函数(首字母转换成⼤写)
作用:将字符串第⼀个字符转换成⼤写
capitalize语法:字符或⼦串.capitalize()
示例:
str2='happiness is a way station between too much and too little.'
print(str2.capitalize())
运行结果:
5.title():函数(每个单词首字母转换成⼤写)
作用:将字符串每个个字符首字母转换成⼤写
capitalize语法:字符或⼦串.title()
示例:
str2='happiness is a way station between too much and too little.'
print(str2.title())
运行结果:
6.lower():函数(每个单词字母转换成小写)
作用:将字符串每个大写字母转换成小写
lower语法:字符或⼦串.lower()
示例:
str2='happiness is a way station between too much and too little.'
title_str2=str2.title()
print(title_str2)
print(title_str2.lower())
运行结果:
6.upper():函数(每个单词字母转换成小写)
作用:将字符串每个大写字母转换成大写
upper语法:字符或⼦串.upper()
示例:
str2='happiness is a way station between too much and too little.'
title_str2=str2.title()
print(title_str2)
lower_str2=title_str2.lower()
print(lower_str2.upper())#将字符串每个大写字母转换成大写
运行结果:
7.删除空白函数
语法:字符或⼦串.函数()
函数:
函数 | 说明 |
---|---|
strip(): | 删除字符串左侧空⽩字符 |
lstrip() | 删除字符串右侧空⽩字符 |
rstrip() | 删除字符串两侧空⽩字符 |
示例:
str2=' happiness is a way station between too much and too little. '
print(str2.strip())#两侧
print(str2.lstrip())#左侧
print(str2.rstrip())#右侧
运行结果:
8。对齐函数
语法:字符或⼦串.函数(⻓度, 填充字符)
函数:
函数 | 说明 |
---|---|
ljust(): | 左对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串 |
rjust() | 原字符串右对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和ljust()相同 |
center() | 返回⼀个原字符串居中对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和ljust()相同 |
示例:
str1='hello'
print(str1.ljust(10,'.'))
print(str1.rjust(10,'+'))
print(str1.center(10,'-'))
运行结果:
4.3判断
4.3.1判断函数
作用:所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False
函数 | 说明 |
---|---|
startswith() | 检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查,语法:字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标) |
endswith() | 检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查语法:字符串序列.endswith(⼦串, 开始位置下标, 结束位置下标) |
isalpha() | 如果字符串⾄少有⼀个字符并且所有字符都是字⺟则返回 True, 否则返回 False,语法:字符串.isalpha() |
isdigit() | 如果字符串只包含数字则返回 True 否则返回 False,语法:字符串.isdigit() |
isalnum() | 如果字符串⾄少有⼀个字符并且所有字符都是字⺟或数字则返 回 True,否则返回False,语法:字符串.isalnum() |
isspace() | 如果字符串中只包含空⽩,则返回 True,否则返回 False,语法:字符串.isspace() |
示例:
startswith()
mystr = "hello world "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 10))
endswith()
mystr = "hello world and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 5))
isalpha()
mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())
isdigit()
mystr1 = 'aaa12345'
mystr2 = '12345'
# 结果: False
print(mystr1.isdigit())
# 结果:False
print(mystr2.isdigit())
isalnum()
mystr1 = 'aaa12345'
mystr2 = '12345-'
# 结果:True
print(mystr1.isalnum())
# 结果:False
print(mystr2.isalnum())
isspace()
mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 结果:False
print(mystr1.isspace())
# 结果:True
print(mystr2.isspace())