Reputation: 47
I get a number (n) from user. Just
n = int(input())
After that, I have to to create n strings and get their values from user.
i = 0;
while (i < n):
word = input() # so here is my problem:
# i don't know how to create n different strings
i += 1
How to create n strings?
Upvotes: 1
Views: 915
Reputation: 41
If you are aware of list comprehensions, you can do this in a single line
s = [str(input()) for i in range(int(input()))]
int(input()) - This gets the input on the number of strings. Then the for loop is run for the input number of iterations and str(input()) is called and the input is automatically appended to the list 's'.
Upvotes: 2
Reputation: 6053
Try this (python 3):
n = int(input())
s = []
for i in range(n):
s.append(str(input()))
The lits s will contains all the n strings.
Upvotes: 2
Reputation: 171
You need to use a list, like this:
n = int(input())
i = 0
words = []
while ( i < n ):
word = input()
words.append(word)
i += 1
Also, this loop is better created as a for loop:
n = int(input())
words = []
for i in range(n):
words.append(input())
Upvotes: 3