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