1

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?

3
  • 2
    you need to think about storing the strings: docs.python.org/2/tutorial/datastructures.html Commented Oct 12, 2016 at 8:50
  • unrelated, but it is better Python style to use for-loops in this case. Commented Oct 12, 2016 at 8:51
  • you can use a list. Commented Oct 12, 2016 at 8:53

3 Answers 3

3

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())
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.