find()# 方法
find()# 范围查找子串,返回索引值,找不到返回-1

# 语法
s.find(substring, start=0, end=len(string)) 
# 参数
# substring -- 指定检索的字符串
# start -- 开始索引,默认为0。
# end -- 结束索引,默认为字符串的长度。

# 示例
s = 'python'
s.find('th')
# 2
s.find('th',1,2)
# -1
s.find('th',1,3)
# -1
s.find('th',1,4)
# 2
s.find('tb',1,4)
# -1
count()方法
# 统计字符串里某个字符出现的次数。

# 语法
s.count(substring, start= 0,end=len(string))
# 参数
# sub -- 搜索的子字符串
# start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
# end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。

# 示例
s = 'password'
s.count('s')
# 2
s.count('s',0,2)
# 0
s.count('s',0,3)
# 1
join()# 方法
join() # 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

# 语法
s.join(sequence)
# 参数
# sequence--要生成字符串的元素序列。

# 示例
ls = ['p', 'y', 't', 'h', 'o', 'n']
s = '' # 拼接的字符
s.join(ls)
# 'python'
s  = '-'
s.join(ls)
# 'p-y-t-h-o-n'
# replace()方法
# replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串)

# 语法
s.replace(old, new[, max])

# 参数
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次
# 示例

s = 'hello word'
s.replace('word', 'python')
'hello python'
s = 'hello-word'
s.replace('-', '')
'helloword'
s.replace('-', ' ')
'hello word'
strip()方法
strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。

# 语法
s.strip([chars])
# 参数
chars -- 移除字符串头尾指定的字符序列。

# 示例
s = '  abcdefg   '
s.strip()
'abcdefg'
s = '  123abcdefg123   '
s.strip('123')
'  123abcdefg123   '
s = '123abcdefg123'
s.strip('123')
'abcdefg'
# 说明
# 如果字符中前后有空格需新去掉空格才能移除指定的字符

split()方法
split()通过指定分隔符对字符串进行切片

# 语法
s.split(str="", [num])
# 参数
str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num -- 分割次数。

# 示例
s = 'this is string'
s.split()
['this', 'is', 'string']
s.split('s', 1) # 切割一次
['thi', ' is string']
# 去除字符串空格的方法
# 1、使用字符串函数replace()

>>> s = '  hello world   '

>>> s.replace(' ',  '')
'helloworld'
# 2、使用字符串函数split()

>>> s = ' hello world '
>>> s = ' '.join(s.split())
>>> print(s)
helloworld
# eval()方法
# eval() 函数用来执行一个字符串表达式,并返回表达式的值。

# 示例
def eval_test():
    l='[1,2,3,4,[5,6,7,8,9]]'
    d="{'a':123,'b':456,'c':789}"
    t='('a', 'b', 'c', 'd')'
    a = '2 * 3'
    print '--------------------------转化开始--------------------------------'
    print type(l), type(eval(l))
    print type(d), type(eval(d))
    print type(t), type(eval(t))
    print(eval(a))
if __name__=="__main__":
    eval_test()
# 输出结果


--------------------------转化开始--------------------------------
<type 'str'> <type 'list'>
<type 'str'> <type 'dict'>
<type 'str'> <type 'tuple'>
6
# 其他方法
s.capitalize() # 首字母大写

s.lower() # 转小写

s.upper() # 转大写

s.swapcase() # 大小写互换

len(str) # 字符串长度

cmp(“my friend”,str) # 字符串比较 第一个大,返回1 按ascii码表比较

max('abcxyz') # 寻找字符串中最大的字符

min('abcxyz') # 寻找字符串中最小的字符

s.startswith()('ab') # 判断字符串以是否已ab开头

s.endwith('xz') # 判断字符串是否已xz结尾