赞
踩
目录
s = "hello world"
print(s[0]) #h ,索引从0开始
print(s[-1]) #d ,-1表示末尾
print(s[-3]) #r ,-3倒数第三个字母
print(s[0:4]) #hell 注意左闭右开的区间
print(s[7:-1]) #orl 注意左闭右开的区间
print(s[:]) #hello world 若头下标和尾下标都缺省,则取整个字符串
print(s[:5]) #hello 头下标缺省,表示从字符串的开始取子串
print(s[0:5:2]) #hlo 格式【头下标:尾下标:步长】
find() #查找字符串首次出现的位置,若不存在则返回-1
rfind() #查找字符串指定范围内中最后一次出现的位置,若不存在则返回-1
index() #查找字符串指定范围内中首次出现的位置,若不存在则抛出异常
rindex() #查找字符串指定范围内中最后一次出现的位置,若不存在则抛出异常
count() #用来返回一个字符串在另一个字符串中出现的次数,若不存在则返回0
string.find(str, beg=0, end=len(string))
检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
s = "你好哇,今天天气真好"
s = s.find('好')
string.
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。