user512512af
user512512af

Reputation: 37

Input multiple string in list - python

Hello so im new on python, i want to know how to do multiple string input on list. I already try to append the input to the list, but it doesn't give me expected output. Here is the source code:

test=[]
input1=input("Enter multiple strings: ")
splitinput1=input1.split()
for x in range(len(splitinput1)):
    test.append(splitinput1)
print(test)
print(len(test))

And the output is not what i expected:

Enter multiple strings:  A B C
[['A', 'B', 'C'], ['A', 'B', 'C'], ['A', 'B', 'C']]
3

However, when i change to print(splitinput1), it give me expected output:

Enter multiple strings:  A B C
['A', 'B', 'C']
3

So how to make the output like print(splitinput1) while use print(test) and whats missing on my code? Thankyou.

Upvotes: 0

Views: 1543

Answers (2)

Omer Dagry
Omer Dagry

Reputation: 567

strings = input("Enter multiple strings: ")
test = strings.split()
print(test)
print(len(test))

Upvotes: 1

BishwashK
BishwashK

Reputation: 171

You have slight error in your code. Do this:

test=[]
input1=input("Enter multiple strings: ")
splitinput1=input1.split()
for x in splitinput1:
   test.append(x)
print(test)
print(len(test))

Upvotes: 1

Related Questions