Search code examples
pythonstringfor-loopprintinguser-input

How to add spaces into a developing variable after making a range function


I'm writing a program that asks how many names a user has. Then, using a for loop, I would ask the user for each of their names. And finally, print their full name. All I know is that I should keep a running total of strings and that I can make a string variable to contain the information like my_string = "". That's what I'm having a little trouble with.

The problem I have is that I don't know how to put the spaces in between strings inside a variable as it develops inside the loop. In my code, the names print, but it's all together. So if I typed "John" and "Smith", it would print "JohnSmith" as the full_name variable I have. I've already tried adding "" inside the full_name variable which is in the loop, but nothing changed.

full_name = ""

names = int(input("How many names do you have?: "))

for i in range(names):
    next_name = input("Name: ")
    full_name = full_name + next_name + ""

print(full_name)

I know this is beginner stuff, but I started learning Python like a week ago. Thank you.


Solution

  • This is what you need, I think.

    full_name = ""
    
    names = int(input("How many names do you have?: "))
    
    for i in range(names):
        next_name = input("Name: ")
        full_name = full_name + " " + next_name
    
    print(full_name)