字符串和正则表达式

1、Str 的常用操作

1.1字符串的拼接

>>> a = 'hello'
>>> b = 'python'
>>> a + '-' + b
'hello-python'
>>> a + b
'hellopython'

但是字符串和非字符串不能拼接
在这里插入图片描述

要想拼接得使用字符串的类型转换 str()

>>> a + str(123)
'hello123'

1.2 长度计算 len()

>>> len('我要好好学习python')
12

1.3 截取

字符串是一种特殊的序列,对于列表和元组对它同样适用

>>> a = 'hello'
>>> b = 'python'
>>> a[1]
'e'
>>> 'aaabbbccc'[8]
'c'
>>> b[-2]
'o'
>>> a[1:2]
'e'
>>> a[1:3]
'el'
>>> a[:4]
'hell'

1.4 分隔 split()

用空格和逗号

>>> '1 2 3 4 5'.split()
['1', '2', '3', '4', '5']
>>> '1  2  3  4  5'.split()
['1', '2', '3', '4', '5']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值