1

I am trying to create a very simple program for averaging numbers. And one part of it involves converting an input string into an array.

E.g.

average = input()

average becomes 'Hello world! This is a string.'

I then need to split it into an array like this:

average = ['Hello', 'world!', 'This', 'is', 'a', 'string.']

What would be a way of going about this?

David Wade
  • 29
  • 1
  • 1
  • 2

2 Answers2

1

Simply use the split function :

lst = txt.split(" ")
Itamar
  • 157
  • 9
0
separator = " "
average = input()
print average.split(separator)

There is a inbuilt method called split() which will get your job done , you can pass a separator variable to that method indicating the character you want to split upon However the default value is space " " itself , averge.split() would also work fine in your case.

ZdaR
  • 22,343
  • 7
  • 66
  • 87