从字符串中获取子字符串?

时间:2016-10-26 16:56:08

标签: python string python-2.7

在python中有一种方法可以提取字符串中的每个子字符串吗?

例如,如果我有字符串

"Hello there my name is Python" 

我想从这个字符串中取出每个子字符串(或单个字),以便我从这个字符串中取出"Hello", "there" , "my" , "name" , "is""Python"

2 个答案:

答案 0 :(得分:0)

我相信你要找的是split方法。

它将使用指定的分隔符破坏字符串。默认的分隔符是一个空格。

input_string = "Hello there my name is Python" 
for substring in input_string.split():
    print(substring)

答案 1 :(得分:0)

使用split()字符串方法。

>>> sentence = 'Hello there my name is Python'
>>> words = sentence.split()
>>> print words
['Hello', 'there', 'my', 'name', 'is', 'Python']
相关问题