Home > OS >  Input multiple string in list - python
Input multiple string in list - python

Time:10-25

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.

CodePudding user response:

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

CodePudding user response:

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))
  • Related