Python学习:字符串

一、字符串创建

我们⼀般使⽤引号来创建字符串。创建字符串很简单,只要为变量分配⼀个值即可。

a = 'hello world'
print(type(a))
name1 = """ Rose """
name2= ''' Tom '''#三引号形式的字符串⽀持换⾏。

二、字符串输入输出

(1)字符串输出

name = 'Tom'
print('我的名字是%s' % name)

(2)字符串输入

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

三、下标

“下标” ⼜叫 “索引” ,就是编号。下标从0开始

name = "abcdef"
print(name[1])
print(name[0])
print(name[2])#输出为b a c

四、切片

切⽚是指对操作的对象截取其中⼀部分的操作。字符串、列表、元组都⽀持切⽚操作。
格式为:
序列[开始位置下标:结束位置下标:步⻓]
(1)不包含结束位置下标对应的数据, 正负整数均可;
(2) 步⻓是选取间隔,正负整数均可,默认步⻓为1

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
print(name[::-1]) # gfedcba

五、常用操作方法

(1)find():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返回-1。

str = "hello world and itcast and itheima and Python"
print(str.find('and')) # 12

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

str = "hello world and itcast and itheima and Python"
print(str.index('and')) # 12
print(str.index('ands')) # 报错

(3)rfind(): 和find()功能相同,但查找⽅向为右侧开始。
(4)rindex():和index()功能相同,但查找⽅向为右侧开始。
(5)count():返回某个⼦串在字符串中出现的次数

str = "hello world and itcast and itheima and Python"
print(str.count('and')) # 3
print(str.count('ands')) # 0
print(str.count('and', 0, 20)) # 1

(6)replace():替换,替换次数如果超出⼦串出现次数,则替换次数为该⼦串出现次数。

str = "hello world and itcast and itheima and Python"
# 结果:hello world he itcast he itheima he Python
print(str.replace('and', 'he'))
# 结果:hello world he itcast he itheima he Python
print(str.replace('and', 'he', 10))
# 结果:hello world and itcast and itheima and Python
print(str) 

注:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候不能改变原有字符串,属于不可变类型。
(7)split():按照指定字符分割字符串。
格式为:字符串序列.split(分割字符, num) ,
num表示的是分割字符出现的次数,即将来返回数据个数为num+1个。

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

(8)join():⽤⼀个字符或⼦串合并字符串,即将多个字符串合并为⼀个新的字符串。

list = ['c', 'z', 'b', 'k']
t1 = ('a', 'b', 'c', 'd')
# 结果:c_z_b_k
print('_'.join(list))
# 结果:a...b...c...d
print('...'.join(t1))

(9)capitalize():将字符串第⼀个字符转换成⼤写。

str = "hello world "
# 结果:Hello world 
print(str.capitalize())#capitalize()函数转换后,只字符串第⼀个字符⼤写,其他的字符全都⼩写。

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

str = "hello world "
# 结果:Hello World 
print(str.title())

(11)lower():将字符串中⼤写转⼩写。

str = "Hello World"
# 结果:hello world 
print(mystr.lower())

(12)upper():将字符串中⼩写转⼤写。

str = "hello world "
# 结果:HELLO WORLD 
print(mystr.upper())

(13)lstrip():删除字符串左侧空⽩字符。
(14)rstrip():删除字符串右侧空⽩字符。
(15)strip():删除字符串两侧空⽩字符。
(16)ljust():返回⼀个原字符串左对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串。
格式为:

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

(17)rjust():返回⼀个原字符串右对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,格式和ljust()相同。
(18)center():返回⼀个原字符串居中对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,格式和ljust()相同。
(19)startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
格式为:

字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标) 
str = "hello world"
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))

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

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

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

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

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

mystr1 = 'a12'
mystr2 = '12'
# 结果: False
print(mystr1.isdigit())
# 结果:False
print(mystr2.isdigit())

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

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

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

str1 = '1 2 3'
str2 = ' '
# 结果:False
print(str1.isspace())
# 结果:True
print(str2.isspace())
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python中,可以使用切片(slice)来截取字符串。切片是指从一个字符串中获取指定范围的子字符串。切片的语法是通过使用方括号[]来指定起始位置和结束位置的索引。 下面是一些常见的字符串截取操作: 1. 截取单个字符: 可以通过指定字符的索引来截取单个字符。索引从0开始,负数索引表示从字符串末尾开始计数。 示例代码: ```python string = "Hello, World!" char = string # 获取第一个字符 print(char) # 输出:H ``` 2. 截取子字符串: 可以通过指定起始位置和结束位置的索引来截取子字符串。截取的范围是左闭右开区间,即包含起始位置的字符,但不包含结束位置的字符。 示例代码: ```python string = "Hello, World!" substring = string[7:12] # 获取从索引7到索引11的子字符串 print(substring) # 输出:World ``` 3. 省略起始位置或结束位置: 如果省略起始位置,则默认从字符串的开头开始截取;如果省略结束位置,则默认截取到字符串的末尾。 示例代码: ```python string = "Hello, World!" substring1 = string[:5] # 获取从开头到索引4的子字符串 substring2 = string[7:] # 获取从索引7到末尾的子字符串 print(substring1) # 输出:Hello print(substring2) # 输出:World! ``` 4. 使用负数索引: 可以使用负数索引来从字符串末尾开始计数。例如,-1表示最后一个字符,-2表示倒数第二个字符,依此类推。 示例代码: ```python string = "Hello, World!" substring = string[-6:-1] # 获取从倒数第6个字符到倒数第2个字符的子字符串 print(substring) # 输出:World ``` 希望以上内容对你有所帮助!如果还有其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值