Python字符串

字符串

一、认识字符串

字符串是Python中常用的数据类型,一般使用引号来创建字符串

1. 字符串定义

# 单引号定义字符串
In [1]: s='Hello world!'

In [2]: print(type(s))
<class 'str'>

# 双引号定义字符串
In [3]: s="Hello world!"

In [4]: print(type(s))
<class 'str'>

# 三引号定义字符串
In [6]: s="""Hello world!"""

In [7]: print(s)
Hello world!

In [8]: print(type(s))
<class 'str'>

2. 字符串输出

In [14]: print("Hello world!")
Hello world!

In [15]: s='Hello world!'

In [20]: print('%s'%s)
Hello world!

In [22]: print(f'字符串s是:{s}')
字符产s是:Hello world!

3. 字符串输入

In [23]: s=input('请输入字符串:')
请输入字符串:Hello world!

In [24]: print(type(s))
<class 'str'>

二、索引

  • 数据存储在内存当中,当输入字符串时,这些字符数据会从0开始编号,我们通过下标可以访问某个字符数据
In [25]: str1="Hello world"

In [26]: print(str1[0])
H

In [27]: print(str1[1])
e

In [28]: print('%s,%s'%(str1[0],str1[3]))
H,l

三、切片

1. 语法

序列[开始位置下标:结束位置下标:步长]
  • 不包含结束位置下标对应的数据,正负整数均可
  • 步长是选取间隔,正负整数均可,默认步长为1

2. 举例

In [29]: str1="Hello world"

In [30]: print(str1[6:11:1])
world

In [31]: print(str1[0:6:1])
Hello 

# 省略步长,步长默认为1
In [32]: print(str1[0:6])
Hello 

# 省略起始下标,默认从0开始
In [33]: print(str1[:6])
Hello 

# 省略结束下标,默认到字符串结束位置
In [34]: print(str1[6:])
world

# 省略起始和结束下标,则截取整个字符串
In [35]: print(str1[:])
Hello world

In [37]: str1='0123456789'

# 步长选取-1:则表示倒序截取
In [38]: print(str1[::-1])
9876543210

# 从倒数第四的位置截取到倒数第一的位置
In [39]: print(str1[-4:-1])
678

# 需要注意,-4到-1表示从左向右选取,而步长-1表示从右向左选取,此时两个选取方向冲突,则没有输出
In [40]: print(str1[-4:-1:-1])

# -1到-4选取方向与步长选取方向一致,则会正常执行
In [41]: print(str1[-1:-4:-1])
987

四、常用操作

1. 查找

  • 检测某个子串是否包含在这个字符串中,如果在则返回这个子串的其实位置下标,否则返回-1

  • 语法

字符串序列.find(子串,开始位置下标,结束位置下标)
  • 开始和结束位置下标省略时表示在整个序列查找

  • 举例使用:

In [42]: str1='0123456789'

In [43]: print(str1.find('345'))
3

# 在区间0到5查找,没有则返回-1
In [44]: print(str1.find('67',0,5))
-1

# rfind()表示从右侧开始查找,同样rindex()也表示从右侧查找
In [45]: str1='0123 and 456 and 789'

In [46]: print(str1.rfind('and'))
13

In [47]: str1='0123 and 456 and 789'
    
# count()函数统计子串出现的次数
In [49]: print(str1.count('and'))
2
  • index():的用法和find()的用法一样,需要注意的是index()在查找时,如果不存在则会报错,此处不再演示

2. 修改

  • replace():替换
字符串序列.replace(旧子串,新子串,替换次数)
  • 举例
In [50]: str1='0123 and 456 and 789'

In [51]: str1.replace('and','he')
Out[51]: '0123 he 456 he 789'

# 原有字符串并不会改变
In [52]: print(str1)
0123 and 456 and 789

# replace()函数有返回值
In [53]: new_str1=str1.replace('and','he')

In [54]: print(new_str1)
0123 he 456 he 789

In [55]: str1='0123 and 456 and 789'

# 指定替换的次数为1此,则可以看到第一个and被替换
In [56]: str1.replace('and','he',1)
Out[56]: '0123 he 456 and 789'

# 替换次数为2,则两个and都被替换
In [57]: str1.replace('and','he',2)
Out[57]: '0123 he 456 he 789'

# 替换次数大于字符串出现的次数,则会被全部替换
In [58]: str1.replace('and','he',10)
Out[58]: '0123 he 456 he 789'

  • 从以上代码可知,str为不可变数据类型,因为调用replace()函数后原有str1并没有改变

  • split():按照指定字符分割字符串

# num表示分割字符出现的次数,即将来返回数据个数为num+1个
字符串序列.split(分割字符,num)
  • 举例
In [59]: str1='0123 and 456 and 789'

# 用字符串and进行分割,返回的为一个列表
In [60]: list1=str1.split('and')

In [61]: print(list1)
['0123 ', ' 456 ', ' 789']

In [62]: str1='0123 and 456 and 7 and 89'

# 执行分割次数为2,则会返回3个子串
In [63]: list1=str1.split('and',2)

In [64]: print(list1)
['0123 ', ' 456 ', ' 7 and 89']
  • join():合并列表中的字符串数据为一个大字符串
# 语法
字符或子串.join(多字符串组成的序列)
  • 举例
In [65]: str1='0123 and 456 and 789'

# 通过split分割,返回一个列表
In [66]: list1=str1.split('and')

# 将列表以'...'进行连接,返回一个新的字符串
In [67]: new_str='...'.join(list1)

In [68]: print(new_str)
0123 ... 456 ... 789

3. 其他

  • capitalize():将字符串第一个字符转换为大写
In [69]: str1='hello world'

# 将第一个字符转换为大写,其他都为小写
In [71]: print(str1.capitalize())
Hello world
  • title():将字符串每个单词首字母转换成大写
In [72]: str1='hello world'

# 每个单词首字母都转为大写
In [73]: print(str1.title())
Hello World
  • upper():将小写转为大写
In [74]: str1='hello world'

In [75]: str1.upper()
Out[75]: 'HELLO WORLD'
  • lower():将大写转小写
In [76]: str1='hello WORLD'

In [77]: str1.lower()
Out[77]: 'hello world'
  • lstrip():删除字符串左侧空白字符

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

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

In [78]: str1='  hello world  '

In [79]: str1.lstrip()
Out[79]: 'hello world  '

In [80]: str1.rstrip()
Out[80]: '  hello world'

In [81]: str1.strip()
Out[81]: 'hello world'
  • ljust():返回一个原字符串左对齐,并使用指定字符(默认空格),填充至对应长度的新字符串

  • rjust():返回一个原字符串右对齐,并使用指定字符(默认空格),填充至对应长度的新字符串

  • center():返回一个原字符串中间对齐,并使用指定字符(默认空格),填充至对应长度的新字符串

In [84]: str1='hello'

In [85]: str1.ljust(10)
Out[85]: 'hello     '

In [86]: str1.ljust(10,'.')
Out[86]: 'hello.....'

In [87]: str1.rjust(10,'.')
Out[87]: '.....hello'

In [89]: str1.center(10)
Out[89]: '  hello   '

In [91]: str1.center(11,'.')
Out[91]: '...hello...'

4. 判断

  • startswith():检查字符串是否是以指定子串开始,是则返回True,否则返回False。如果设置开始和结束下标,则在指定范围内检查
字符串序列.startswith(子串,开始位置下标,结束位置下标)
  • 举例
In [93]: str1.startswith('hello')
Out[93]: True

# 设置开始下标为6,省略结束下标
In [94]: str1.startswith('hello',6)
Out[94]: False
  • endswith():检查字符串是否是以指定子串结束,是则返回True,否则返回False。如果设置开始和结束下标,则在指定范围内检查
In [102]: str1='hello world'

In [103]: str1.endswith('world')
Out[103]: True

In [104]: str1.endswith('world',0,5)
Out[104]: False
  • isalpha():如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False

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

  • isalnum():如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False

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

In [113]: str1='hello123'

In [114]: str2='hello'

In [115]: str3='1234'

In [116]: str4=' '

In [117]: str1.isalpha()
Out[117]: False

In [118]: str2.isalpha()
Out[118]: True

In [119]: str2.isdigit()
Out[119]: False

In [120]: str3.isdigit()
Out[120]: True

In [121]: str3.isalnum()
Out[121]: True

In [122]: str4.isspace()
Out[122]: True

In [123]: str3.isspace()
Out[123]: False

五、其他

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ysw!不将就

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值