pythoninputstring-formattingf-string

Include a variable inside a INPUT statement


I'm writing a Python script that asks the user to type multiple names. I want my input prompt to include the current number position of the name being requested (For example: Type the first name... Second name... And so on)

This is how I did it:

x = int(input("Numbers of names that'll be written: "))

for count in range(x):
    name = str(input("Insert the name number {}".format(count))) # <- This works but it's not what i want to achieve, since i don't know how to add a text after the .format
    # name = str(input("Insert the ",count,"st name: )) <- This doesn't work 

I expected the following output:

Insert the 1st name; Insert the 2nd name; Insert the 3nd name;

How can I include a variable and text together inside a input() statement, in Python?


Solution

  • i don't know how to add a text after the .format

    Just like you have text before the {}, you can have text after it.

    str(input("Insert the name number {}, whatever should come after 'cont'".format(cont)))