简体   繁体   中英

How to add commas and periods next to variables in print statement

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.

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#

您确实应该像@Sandsten 上面所说的那样使用 f 字符串,但是对于您的信息,有如下旧方法...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM