-1

Is there a way in python to extract each substring thats inside a string?

For example if I have the string

"Hello there my name is Python" 

I want to take out each sub-string (or individual word) from within this string so that I have "Hello", "there" , "my" , "name" , "is" and "Python" each taken out of this string?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
slowjoe44
  • 7
  • 3

2 Answers2

0

I believe what you are looking for is the split method.

It will break the string with specified delimeter. Default delimeter is a space.

input_string = "Hello there my name is Python" 
for substring in input_string.split():
    print(substring)
Dmitry Yantsen
  • 1,145
  • 11
  • 24
0

Use the split() string method.

>>> sentence = 'Hello there my name is Python'
>>> words = sentence.split()
>>> print words
['Hello', 'there', 'my', 'name', 'is', 'Python']
John Gordon
  • 29,573
  • 7
  • 33
  • 58