Home > Net >  How to add commas and periods next to variables in print statement
How to add commas and periods next to variables in print statement

Time:10-17

I have completed my desired code, but am having trouble with a small syntax issue.

I have some print statements that print what I need, but they have weird spacing and I want it to look like a complete sentence without spaces in between the commas and periods.

Here are the lines:

     if year < 2001:
            print(name, ",", "you were born in", year,".")
        
        elif year > 2100:
            print(name, ",", "you were born in", year,".")
        
        else:
            print(name, ",", "you were born in", year,", which is the 21st century.")

print(name, "'s classification is", classification, ".")

In these lines, name and year are defined from input, I'm just confused on how to get it to run with the correct spacing.

Any help is appreciated!

Thank you so much.

CodePudding user response:

When you separate values with comma in print() it will add spaces. Since you are using Python 3 you could instead use f-strings like this

print(f"{name}, you were born in {year}.")

There's great documentation on it here: https://docs.python.org/3/tutorial/inputoutput.html#

CodePudding user response:

You should indeed use f strings as @Sandsten has said above, but just for your info there is the old way as below...

print('{0}, you were born in {1}.').format(name, year)
  • Related