零基础篇-基础阶段04-字符串

@ 数据序列

一、字符串

(一)认识字符串

字符串是 Python 中最常⽤的数据类型。我们⼀般使⽤引号来创建字符串。创建字符串很简单,只要为
变量分配⼀个值即可

a = 'hello world'
print(type(a))

注意:控制台显示结果为 <class ‘str’> , 即数据类型为str(字符串)。

1.1字符串特征

一对引号创建字符串

name1 = 'Tom'
name2 = "Jack"

三引号字符串

name1 = '''Tom'''
name2 = """Jack"""

注意:三引号形式的字符串⽀持换⾏

1.2字符串输出

print('hello world')

name = 'Tom'
print('我的名字是:',name)
print('我的名字是%s'%name)
print(f'我的名字是{name}')

1.3 字符串输入

在Python中,使⽤ input() 接收⽤户输⼊

name = input('请输入您的名字:')
print( f'您输入的名字是{name}')
print(type(name))

password = int(input('请输入您的密码:'))
print(f'您输入的密码是{password}')
print(type(password))

执行结果
在这里插入图片描述

二、下标

“下标” ⼜叫 “索引” ,就是编号。⽐如⽕⻋座位号,座位号的作⽤:按照编号快速找到对应的座位。同
理,下标的作⽤即是通过下标快速找到对应的数据
字符串中的下标范围是0~字符串个数-1

2.1 快速体验

需求:字符串 name = “abcdef” ,取到不同下标对应的数据

name = 'abcdef'
print(name[1])
print(name[0])
print(name[3])

执行结果
在这里插入图片描述
下标从0开始
在这里插入图片描述

三、切片

切⽚是指对操作的对象截取其中⼀部分的操作。字符串、列表、元组都⽀持切⽚操作

3.1语法

序列[开始位置下标:结束位置下标:步长]

注意:
1 不包含结束位置下标对应的数据, 正负整数均可;
2. 步⻓是选取间隔,正负整数均可,默认步⻓为1
当步长是正数(从前往后取), 开始下标对应的字符要在结束下标对应的字符的前面
当步长是负数(从后往前取),开始下标对应的字符要在结束下标对应的字符的后面

结束下标对应的值取不到的

3.2体验

name = 'abcdefg'
#       0123456

print(name[2:5:1]) # cde 
print(name[2:5]) # cde  默认步长为1
print(name[:5]) # abcde  没有开始从0开始
print(name[1:]) # bcdefg  没有结束,取到结尾
print(name[:]) # abcdefg  
print(name[::2]) # aceg  
print(name[:-1]) # abcdef, 负1表示倒数第⼀个数据
print(name[-4:-1]) # def
print(name[::-1]) # gfedcba

四、常用的操作方法

字符串的常⽤操作⽅法有查找、修改和判断三⼤类

4.1查找

所谓字符串查找⽅法即是查找⼦串在字符串中的位置或出现的次数。
find ()
find():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返
回-1
1.语法

字符串序列.find(子串,开始位置下标,结束位置下标)
注意:开始和结束位置的下标可以省略,表示在整个字符串序列中查找

2.快速体验

mystr = 'hello world and python and python'
print(mystr.find('and'))  # 12
print(mystr.find('and',10,20))  # 12
print(mystr.find('ands'))   # -1

index ()
index():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则
报异常
1.语法

字符串序列.index(⼦串, 开始位置下标, 结束位置下标)

2.快速体验

mystr = 'hello world and python and python'
print(mystr.index('and'))  # 12
print(mystr.index('and',10,20))  # 12
print(mystr.index('ands'))   # ValueError: substring not found

补充
rfind(): 和find()功能相同,但查找⽅向为右侧开始。
rindex():和index()功能相同,但查找⽅向为右侧开始

count ()
返回某个⼦串在字符串中出现的次数
1.语法
字符串序列.count(⼦串, 开始位置下标, 结束位置下标)
2.快速体验

mystr = 'hello world and python and python'
print(mystr.count('and'))  # 2
print(mystr.count('and',10,20))  # 1
print(mystr.count('ands'))   # 0

4.2 修改

所谓修改字符串,指的就是通过函数的形式修改字符串中的数据
replace()
**1.语法

字符串序列.replace(旧⼦串, 新⼦串, 替换次数)
注意:替换次数如果查出⼦串出现次数,则替换次数为该⼦串出现次数

2.快速体验

mystr = 'hello world and python and python'
print(mystr.replace('and','he'))  # hello world he python he python
print(mystr.replace('and','he',10))  # hello world he python he python 超过子串出现的数量,替换该子串出现的次数
print(mystr)

注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候
不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型
split()按照指定字符分割字符串
1.语法

字符串序列.split(分割字符, num)
注意:num表示的是分割字符出现的次数,即将来返回数据个数为num+1个

** 2.快速体验**

mystr = "hello world and hello and python and Python"
# 结果:['hello world ', ' hello ', ' python ', ' Python']
print(mystr.split('and'))
# 结果:'hello world ', ' hello ', ' python and Python']
print(mystr.split('and', 2))
# 结果:['hello', 'world', 'and', 'hello', 'and', 'python', 'and', 'Python']
print(mystr.split(' '))
# 结果:['hello', 'world', 'and hello and python and Python']
print(mystr.split(' ', 2))

注意:如果分割字符是原有字符串中的⼦串,分割后则丢失该⼦串
join()用一个字符或子串合并字符串,即是将多个字符串合并成一个新的字符串
1.语法

字符或⼦串.join(多字符串组成的序列)

2.快速体验

list1 = ['hello','python','nihao','xiaowu']
t1 = ('aa','b','cc','ddd')
print('_'.join(list1)) # hello_python_nihao_xiaowu
print('...'.join(t1))  # aa...b...cc...ddd

capitalize():将字符串第⼀个字符转换成大写

mystr = "hello world and hello xiaowu"
print(mystr.capitalize())
# Hello world and hello xiaowu
# 注意:capitalize()函数转换后,只字符串第⼀个字符⼤写,其他的字符全都⼩写

title():将字符串每个单词⾸字⺟转换成大写

mystr = "hello world and hello xiaowu"
print(mystr.title()) # Hello World And Hello Xiaowu

lower():将字符串中大写转为小写

mystr = "hello wORld and HEllo xiaowu"
print(mystr.lower()) # hello world and hello xiaowu

** upper():将字符串中小写转大写**

mystr = "hello wORld and HEllo xiaowu"
print(mystr.upper())  # HELLO WORLD AND HELLO XIAOWU

lstrip():删除字符串左侧空白字符

mystr = "         hello wORld and HEllo xiaowu"
print(mystr.lstrip()) 

rstrip():删除字符串左侧空白字符
strip():删除字符串两侧空白字符
ljust():返回一个原字符串对齐,并使用指定字符串(默认空格)填充至对应长度的新字符串

1.语法

字符串序列.ljust(⻓度, 填充字符)

2.输出效果

mystr = 'hello'
mystr.ljust(10,'.')
print(mystr.ljust(10,'.'))

rjust():返回⼀个原字符串右对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和
ljust()相同。
center():返回⼀个原字符串居中对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语
法和ljust()相同

4.3判断

所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False
startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开
始和结束位置下标,则在指定范围内检查

1.语法

字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标)

2.快速体验

mystr = 'hello world and xiaowu and python'

# 结果 :True
print(mystr.startswith('hello'))

# 结果:False
print(mystr.startswith('hello',5,20))

endswith()::检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开
始和结束位置下标,则在指定范围内检查

1.语法

字符串序列.endswith(⼦串, 开始位置下标, 结束位置下标)

2.快速体验

mystr = 'hello world and xiaowu and python'

# 结果 :True
print(mystr.endswith('python'))

# 结果:False
print(mystr.startswith('Python'))

# 结果:False
print(mystr.endswith('Python',2,20))

isalpha():如果字符串⾄少有⼀个字符并且所有字符都是字⺟则返回 True, 否则返回 False

mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())

isdigit():如果字符串只包含数字则返回 True 否则返回 False

mystr1 = 'aaa12345'
mystr2 = '12345'
# 结果: False
print(mystr1.isdigit())
# 结果:False
print(mystr2.isdigit())

isalnum():如果字符串⾄少有⼀个字符并且所有字符都是字⺟或数字则返 回 True,否则返回
False

mystr1 = 'aaa12345'
mystr2 = '12345-'
# 结果:True
print(mystr1.isalnum())
# 结果:False
print(mystr2.isalnum())

isspace():如果字符串中只包含空⽩,则返回 True,否则返回 False

mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 结果:False
print(mystr1.isspace())
# 结果:True
print(mystr2.isspace())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值