python中字符串的常见操作
字符串切片
str = "123456789"
# 取字符串的"123"
str[0:3]
# 取字符串"789"
str[6:-1]
# 取字符串"1357"
str[0:-1:2]
# 反转字符串
str[::-1]
字符串查找字符操作
str = "hello world!"
# find()返回值为存在为索引值,反之为-1
str.find("hello",0,12)
# 用index()方法查找,如果没有的话,程序会报异常
str.index("hello",0,12)
# 用count()方法,返回在字符串中出现的次数
str.count("hello")
字符串修改字符
str = "hello world haha!"
# replace()方法"h"-->"H",count=2 转换次数2
str.replace("h","H",2)
# split(),分割符切片
str.split()
str.split(" ", 2)
字符串增加字符
str = "hello world!"
# join()方法把字符加在str每个元素之间
str.join("-")