第一种方法:in
string = ‘helloworld‘
if ‘world‘ in string:
print ‘Exist‘
else:
print ‘Not exist‘
第二种方法:find
string = ‘helloworld‘
if string.find(’world‘) == 5: #5的意思是world字符从那个序开始,因为w位于第六个,及序为5,所以判断5
print ‘Exist‘
else:
print ‘Not exist‘
第三种方法:index,此方法与find作用类似,也是找到字符起始的序号
if string.index(’world‘) > -1: #因为-1的意思代表没有找到字符,所以判断>-1就代表能找到
print ‘Exist‘
如果没找到,程序会抛出异常
----------------------------------------------------------------------------------------------------------------------------------
>>> a = " a b c "
>>> a.strip()
‘a b c‘
>>> a = " a b c "
>>> a.lstrip()
‘a b c ‘
>>> a = " a b c "
>>> a.rstrip()
‘ a b c‘
# replace主要用于字符串的替换replace(old, new, count)
>>> a = " a b c "
>>> a.replace(" ", "")
‘abc‘
# join为字符字符串合成传入一个字符串列表,split用于字符串分割可以按规则进行分割
>>> a = " a b c "
>>> b = a.split() # 字符串按空格分割成列表
>>> b [‘a‘, ‘b‘, ‘c‘]
>>> c = "".join(b) # 使用一个空字符串合成列表内容生成新的字符串
>>> c ‘abc‘
# 快捷用法
>>> a = " a b c "
>>> "".join(a.split())
‘abc‘
-----------------------------
做法:
1.str.split() # 单一符号分割
2.filter(None,str.split(" ")) # 多空的分割
原文:https://www.cnblogs.com/gisxiong/p/10830896.html