|
字符串分割:
s.split([sep, [maxsplit]]) #以sep为分隔符,把s分成一个list。maxsplit表示分割次数。默认的分隔符是空白符
s.rsplit([sep, [maxsplit]])
s.plitlines([keepends]) #把S按照行分割符分为一个list,keepends是一个bool值,如果为真每行后而会保留行分割符。
eg:
host = '10.1.74.51:3181,10.1.74.52:3181,10.1.74.49:3181'
hosts = host.split(',')
for i in range(len(hosts)):
print hosts
--------------------------------------------------------------------------------------------------------------
字符串拼接:
s.join(seq)
eg:
s = 'hello'
seq = 'world !'
str = s.join(seq) # str = whello ohello rhello lhello dhello hello !
str1 = s + seq # str1 = hello world !
--------------------------------------------------------------------------------------------------
字符串替换:
str.replace(oldstr,newstr,[count]) #把str 中oldstr替换成newstr,count为替换次数
str.strip([chars]) #把chars中含的字符全部去掉,可以理解为替换为None
str.lstrip([chars])
str.rtrip([chars])
eg:
去掉字符串中的特殊字符
str = ' \t\nhello \t\nworld ! '
str = str.strip().str.lstrip('\t').rstrip('\n')
# str = 'hello world!'
-------------------------------------------------------------------------------------------------
字符串查找:
str.find(substr,[start,[end]]) 返回str 中出现substr第一个字母的下标,如果str中没有substr 则返回-1
str.index(substr,[start,[end]])与find作用一样,只是在str中不出现substr时会返回一个运行时错误
str.rfind(substr,[start,[end]]) 返回str 中出现substr第一个字母的下标,如果str中没有substr 则返回-1
eg:
s = 'hello world'
index = s.find('wor') # index = 6
----------------------------------------------------------------------------------------------------
字符串比较:
cmp(str1,str2) #若 str1 = str2 返回0 否则返回 -1
eg:
A = 'aaaaa'
B = 'bbbbb'
C = 'aaaaa'
print cmp(A,B) # false
print cmp(A,C) # true |
|
|