Python字符串常用方法

Python字符串常用方法


1. len()
获取字符串长度

s = 'hello'
print(len(s)) # 5

2. find()
查找指定内容在字符串中是否存在, 如果存在就返回该内容在字符串中第一次出现的索引值

s = 'hello'
print(s.find('l')) # 2

3. startswith(), endwith()
判断字符串是否以__为开头或者结尾
返回true/false

s = 'hello'
print(s.startswith('c')) # true
print(s.endwith('a')) # false

4. count()
统计字符串中某字符出现的次数

s = 'aaabbb'
print(s.count('a')) # 3

5. replace(__old, __new,__count)
替换字符串中指定的内容, 如果指定次数count,则替换不会超过count次

s = 'cccdd'
print(s.replace('c', 'd')) # ddddd

6. split()
通过参数内容切割字符串

s = '1#2#3#4'
print(s.split('#'))

7. upper(), lower()
将字符串转换为大/小写

s = 'hello'
print(s.upper()) # HELLO

8. strip()
去除空格

s = '   a   '
print(s.strip()) # a

9. join()
将序列(列表或元组)中的元素以指定的字符连接成一个新的字符串

str = ','
seq = ['hello', 'world']
print(str.join(seq)) # hello,world

Python列表常用方法:
https://blog.csdn.net/swe50/article/details/121980186

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值