0
点赞
收藏
分享

微信扫一扫

python 字符串切片、大小写转化、首字母大写、以什么开始结束、字符串包含类型、切割、替换、删除空白、对齐、合并、查找

天蓝Sea 2023-02-21 阅读 104


判断字符串是否以某个子串开始结尾

  • ​endswith​​ 判断字符串是否以某个子串结尾

a = 'spider.py'
a = a.endswith('.py')

True

  • ​startswith​​ 判断字符串是否以某个子串开头

a = 'http://www.baidu.com'
a = a.startswith('http:')

True

字符串首字母操作

  • ​capitalize()​​ 字符串首字母大写

a = 'life is short , you need Python '
a.capitalize()

Life is short , you need python

  • ​title()​​ 字符串中每个单词首字母大写

a = 'life is short , you need Python '
a.title()

Life Is Short , You Need Python

大小写转化

  • ​upper()​​ 小写转大写

a = 'abcedfg'
a = a.upper()

ABCEDFG

  • ​lower()​​ 大写转小写

a = 'aBeDfG'
a = a.lower()

abcdef

判断字符串类型

  • ​isalpha()​​ 判断是否字母

a = 'abc'
a.isalpha()

True

  • ​isdigit()​​判断是否数字

a = '12345'
a.isdigit()

True

  • ​isalnum()​​ 判断数字或字母或组合

a = 'abc123'
a.isalnum()

True

  • ​isspace()​​判断空白

a = '   '
a.isspace()

True

字符串切割,替换

  • ​split​​分割,返回一个列表, 丢失分割字符

a = "abc;def;ghi"
a = a.split(';')

['abc', 'def', 'ghi']

  • ​replace​​ 替换,replace('旧字, ‘新字’)

a = '人生几河,我用python'
a = a.replace('河', '何')

人生几何,我用python

字符串删除空白字符

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

a = '    abcdef  '.
a = a.strip()

abcdef

  • ​lstrip()​​ 删除左侧空白字符

a = '    abcdef'.
a = a.lstrip()

abcdef

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

a = 'abcdef    '.
a = a.rstrip()

abcdef

字符串对齐

  • ​ljust()​​ 文本字符串左边对齐(可接受填充字符)

a = 'Life is short,you need Python'
a = a.ljust(20)

python 字符串切片、大小写转化、首字母大写、以什么开始结束、字符串包含类型、切割、替换、删除空白、对齐、合并、查找_Python

  • ​rjust()​​ 文本字符串右边对齐(可接受填充字符)

a = 'Life is short,you need Python'
a = a.rjust(50)

python 字符串切片、大小写转化、首字母大写、以什么开始结束、字符串包含类型、切割、替换、删除空白、对齐、合并、查找_字符串_02

a = 'Life is short,you need Python'
a = a.rjust(50, '>')

python 字符串切片、大小写转化、首字母大写、以什么开始结束、字符串包含类型、切割、替换、删除空白、对齐、合并、查找_Python_03

  • ​rjust()​​ 文本字符串居中对齐(可接受填充字符)

a = 'Life is short,you need Python'
a = a.center(50, '>')

python 字符串切片、大小写转化、首字母大写、以什么开始结束、字符串包含类型、切割、替换、删除空白、对齐、合并、查找_字符串_04

字符串合并

  • ​join()​​ 合并列表里面的字符串数据为一个大字符串(可接受填充字符)

a = ['Life', 'is', 'short', 'you', 'need', 'Python']
a = ''.join(a)

LifeisshortyouneedPython

a = ['Life', 'is', 'short', 'you', 'need', 'Python']
a = ' '.join(a)

Life is short you need Python

  • ​format()​​ 字符串合并

a = 'Life is {} , you need{} '.format('short', 'Python')

Life is short , you needPython

  • ​f​​ 字符串合并

m = 'short'
n = 'Python'
a = f'Life is {m} , you need {n} '

Life is short , you need Python

  • ​%​​ 字符串合并 %s 字符串, %d数字,%f浮点

a = 'Life is %s , you need %s ' % ('short', 'Python')

Life is short , you need Python

查找方法

a = 'Life is short , you need Python '

  • ​find()​​ 存在是否存在,存在则返回下标, 不存在-1

a.find('is')

5

  • ​.rfind()​

a.rfind('e')

22 从有右侧开始

  • ​.index()​​ 获取元素的下标,存在返回下标, 不存在保存

a.index('is')

a.index('is',15,20)

  • ​.rindex()​

a.rindex('is')

a.rindex('is',15,20)

  • ​count()​​出现的频率

a.count('s')

2 s出现的次数

切片操作

​字符串[开始索引:结束索引:步长]​​​​切取字符串​​​为开始索引到结束索引-1内的字符串
​​​步长​​不指定时步长为1 字符串[开始索引:结束索引]

a = 'abcdefghijqmn'

  • 下标即索引

a[0]   索引为零的元素  a

  • 截取2 - 6位置的字符(前包含,后不包含)

a[2:6]

cdef

  • 开始索引可以省略不写

a[:6]

abcdef 0-6之间字符串

  • 结束索引可以省略不写

a[2:]

cdefghijqmn 2-结束的字符

  • 完整字符串

a[:]

abcdefghijqmn

  • 开始负数,无结束

a[-2:]

mn 从倒数第几个至结束

  • 开始正数,结束为负数

a[2:-2]

cdefghijq 为两个数之间

  • 开始负数数,结束为负数

a[-5:-2]

ijq 两个负数之间

  • 从开始位置,每隔一个字符截取字符串

a[::2]

acegiqn

  • 从索引1开始,每隔一个取一个

a[1::2]

bdfhjm

  • 字符串的逆序

a[::-1]

nmqjihgfedcba

  • 字符串的逆序,隔一个取一个

a[::-2]
nqigeca


举报

相关推荐

0 条评论