一、认识字符串
字符串是 Python 中最常用的数据类型。我们一般使用引号来创建字符串。创建字符串很简单,只要为变量分配一个值即可。
1 a = 'helloworld'
2 b ="abcdefg"
3 print(type(a))
4 print(type(b))
注意:控制台显示结果为 <class 'str'>, 即数据类型为str(字符串)
1.1字符串特征
- 一对引号字符串
name1='Tom'
name2="Tom"
- 三对引号字符串
name3='''Tom'''
思考:如果创建一个字符串 I’m Tom?
name="I'm Tom"
1.2 字符串输出
name = 'Tom'
print('我的名字是%s' % name)
print(f'我的名字是{name}')
字符串输入
在Python中,使用input()接收用户输入
print(input('请输入名字:'))
print(input('请输入密码:'))
- 输出结果

二、下标
“下标” 又叫 “索引” ,就是编号。比如火⻋座位号,座位号的作用:按照编号快速找到对应的座位。同理,下标的作用即是通过下标快速找到对应的数据。
2.1 快速体验
需求:字符串 name = “abcdef”,取到不同下标对应的数据
name='abcdefg'
print(name[1])
print(name[0])
print(name[2])
- 结果

- 注意:下标从0开始
三、切片
切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。
3.1 语法
序列[开始位置下标:结束位置下标:步⻓]
不包含结束位置的下标
3.2 体验
name='abcdefg'
print(name[2:5:1]) # cde
print(name[2:5]) # cde
print(name[:5]) # abcde
print(name[1:]) # bcdefg
print(name[:]) # abcdefg
print(name[::2]) #aceg
print(name[:-1]) # abcdef,负1表示倒数第一个数据
print(name[-4:-1]) #def 负1指最后一个数据依次前推
print(name[::-1]) #gfedcba 步长为负1倒序排列
四、常用操作方法
字符串的常用操作方法有查找、修改和判断三大类。
4.1 查找
所谓字符串查找方法即是查找子串在字符串中的位置或出现的次数。
- find():检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则则返 回-1。
- 语法
字符串序列.find(子串, 开始位置下标, 结束位置下标)
2.快速体验
mystr = "hello world and itcast and itheima andPython"
print(mystr.find('and')) #12
print(mystr.find('and',15,30)) #23
print(mystr.find('ands')) #-1
- index():检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则则报异常。
1.语法
字符串序列.index(子串, 开始位置下标, 结束位置下标)
2.快速体验
mystr = "hello world and itcast and itheima andPython"
print(mystr.index('and')) #12
print(mystr.index('and', 15, 30)) # 23 print(mystr.index('ands')) # 报 错
-
rfind(): 和find()功能相同,但查找方向为右侧开始。
-
rindex():和index()功能相同,但查找方向为右侧开始。
-
count():返回某个子串在字符串中出现的次数
1.语法
字符串序列.count(子串, 开始位置下标, 结束位置下标)
2.快速体验
mystr = "hello world and itcast and itheima andPython"
print(mystr.count('and')) #3
print(mystr.count('ands')) #0
print(mystr.count('and',0,20)) #1
4.2 修改
所谓修改字符串,指的就是通过函数的形式修改字符串中的数据。
- replace() 替换
1.语法
字符串序列.replace(旧子串, 新子串, 替换次数)
2.快速体验
mystr = "hello world and itcast and itheima andPython"
print(mystr.replace('and', 'he'))
# 结 果 :hello world he itcast he itheima hePython
print(mystr.replace('and', 'he', 10))
# 结 果 :hello world he itcast he itheima hePython
print(mystr)
# 结 果 :hello world and itcast and itheima andPython
- split() 分割字符串
1.语法
字符串序列.split(分割字符, num)
2.快速体验
mystr = "hello world and itcast and itheima andPython"
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and', 2))
# 结 果 :['hello world ', ' itcast ', ' itheima andPython']
print(mystr.split(' '))
# 结 果 :['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' ', 2))
# 结 果 :['hello', 'world', 'and itcast and itheima andPython']
- join() 用一个字符或子串合并字符串,即是将多个字符串合并为一个新的字符串。合并列表里面的字符串为一个字符串
1.语法
字符或子串.join(多字符串组成的序列)
2.快速体验
l1 = ['wo', 'shi', 'chao', 'ren']
t1 = ('aa', 'b', 'cc','ddd')
print('_'.join(l1))
# 结果:wo_shi_chao_ren
print('...'.join(t1))
# 结果:aa...b...cc...ddd
- capitalize():将字符串第一个字符转换成大写。
mystr = "hello world and itcast and itheima andPython"
print(mystr.capitalize())
# 结 果 :Hello world and itcast and itheima andpython
- title():将字符串每个单词首字⺟转换成大写。
mystr = "hello world and itcast and itheima andPython"
print(mystr.title())
# 结 果 :Hello World And Itcast And Itheima AndPython
- lower():将字符串中大写转小写。
mystr = "hello world and itcast and itheima andPython"
print(mystr.lower())
# 结 果 :hello world and itcast and itheima andpython
- upper():将字符串中小写转大写。
mystr = "hello world and itcast and itheima andPython"
print(mystr.upper())
# 结 果 :HELLO WORLD AND ITCAST AND ITHEIMA ANDPYTHON
- lstrip():删除字符串左侧空白字符。

- rstrip():删除字符串右侧空白字符。

- strip():删除字符串两侧空白字符。

4.3 判断
所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False。
- startswith():检查字符串是否是以指定子串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
1.语法
字符串序列.startswith(子串, 开始位置下标, 结束位置下标)
2.快速体验
mystr = "hello world and Python"
print(mystr.startswith('hello'))
# 结果 True
print(mystr.startswith('hello', 5, 20))
# 结果 False
- endswith()::检查字符串是否是以指定子串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
1.语法
字符串序列.endswith(子串, 开始位置下标, 结束位置下标)
2.快速体验
mystr="Hello World"
print(mystr.endswith(World))
# 结果 True
print(mystr.endswith(world))
# 结果 False
本文详细讲解了Python字符串的基础概念,如引号字符串、三引号字符串的创建,以及如何使用下标和切片获取和修改字符串内容。重点介绍了find(), index(), replace(), split()等核心操作方法,帮助读者掌握字符串处理技巧。
1713

被折叠的 条评论
为什么被折叠?



