-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?

3
  • 2
    You should at least have searched. str.split is what you want Commented Oct 26, 2016 at 16:57
  • a = "Hello there my name is Python" and then b = a.split() Commented Oct 26, 2016 at 16:57
  • docs.python.org/2/library/stdtypes.html#str.split Commented Oct 26, 2016 at 16:57

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

Use the split() string method.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.