0

I need to split all the words of a string in a list. I know that the code below is wrong, but I've only managed to do this.

def split_sentence(sentence):
    new_list = []
    for i in sentence:
        new_list.append(i)
    return new_list


print(split_sentence("This is a test"))

output :

['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't']

wanted output:

['This', 'is', 'a', 'test']
buran
  • 13,682
  • 10
  • 36
  • 61
mep711
  • 11

2 Answers2

0

Use str.split:

"This is a test".split()
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
0

This is what you are looking for:

def split_sentence(sentence):
    new_list = sentence.split()
    return new_list

or, simply:

def split_sentence(sentence):
    return sentence.split()
kbdev
  • 1,225
  • 1
  • 13
  • 33