遇到的实用字符串的操作,遇到一个写一个,以后再系统整理成一篇
1.判断一个字符串中是否含有想要找的字符串:
my_string = "abcdef"
if "abc" in my_string:
has_abc = True
if has_abc == True:
print "String contains string."
这是单个的,如果要满足多个条件,可以用列表存储子字符串,用for来遍历
# 是否有[@,-]等符号
punctuations = ['a', '-']
for punctuation in punctuations:
if punctuation in url:
has_punctuation = True
print has_punctuation
2.匹配字符串的开头和结尾
当然这可以用正则来实现,这里提供另一种方法,python自带的
In[18]: ss = 'hello, mandy, you are beautiful!'
In[19]: print ss.startswith('he')
True
匹配结尾:
In[20]: print ss.endswith('ful')
False