Python DayDayUp —— 字符串操作(二)

一 字符串方法startswith() , hendswith()

  字符串以startswith() , endswith() 两个方法传入的字符串为开头或结束时返回True,否者返回False。如果是要检查字符串开头和结尾是否等于另一个字符串,而不是整个字符串,这些方法就可以代替==。

>>> 'Hello world!'.startswith('Hello')
True
>>> 'Hello world!'.endswith('world')
False
>>> 'Hello world!'.endswith('world!')
True

 

二 字符串join(),split()

  join():将字符串列表中的值链接起来。(使用方法:' '.join())

  split():分割字符串,返回一个列表,默认按照空白字符分割,也可以指定分割字符。(分割符不包含在列表里面)

#join()方法的使用
>>> t = ['Hello','world!','love']
>>> ' '.join(t)
'Hello world! love'
#split()方法
>>> t = 'I like you very much!
>>> t.split()
['I', 'like', 'you', 'very', 'much!']

#以换行符分割
t = '''I am a star,
I am a star,
I am a star,I am a star,
I am a star.'''

t = t.split('/n')
print(t)

 

posted @ 2018-01-29 20:48  未来分析师  阅读(178)  评论(0编辑  收藏  举报