Printing “前的反斜杠字符”;“行”Python3中的打印函数

Printing “前的反斜杠字符”;“行”Python3中的打印函数,printing,python-3.x,Printing,Python 3.x,我在这个问题上遇到了困难。每次我试图在这个程序中得到反斜杠,它只会给我一个错误 我要的线路是 print(day,\\month,\\year"is a magic date") 但不管我多久尝试一次别的东西,似乎都没什么效果。任何建议都会非常有用。您需要告诉Python“\”是一个字符串: >>> print(day, "\\", month, "\\", year, "\\", "is a magic date") 1 \ Sep \ 1978 \ is a magi

我在这个问题上遇到了困难。每次我试图在这个程序中得到反斜杠,它只会给我一个错误

我要的线路是

print(day,\\month,\\year"is a magic date") 

但不管我多久尝试一次别的东西,似乎都没什么效果。任何建议都会非常有用。

您需要告诉Python“\”是一个字符串:

>>> print(day, "\\", month, "\\", year, "\\", "is a magic date") 
1 \ Sep \ 1978 \ is a magic date
您还可以指定sperator:

>>> print(day, "\\", month, "\\", year, "\\", " is a magic date", sep=" ")
1 \ Sep \ 1978 \ is a magic date
或者干脆做:

>>> print(day ,month, year, " is a magic date", sep="\\") 
1\Sep\1978\ is a magic date

您需要告诉Python“\”是一个字符串:

>>> print(day, "\\", month, "\\", year, "\\", "is a magic date") 
1 \ Sep \ 1978 \ is a magic date
您还可以指定sperator:

>>> print(day, "\\", month, "\\", year, "\\", " is a magic date", sep=" ")
1 \ Sep \ 1978 \ is a magic date
或者干脆做:

>>> print(day ,month, year, " is a magic date", sep="\\") 
1\Sep\1978\ is a magic date

作为Oz123已编写内容的替代方案,您可能希望使用字符串格式:

print('{}/{}/{} is a magic date'.format(day, month, year))

作为Oz123已编写内容的替代方案,您可能希望使用字符串格式:

print('{}/{}/{} is a magic date'.format(day, month, year))

你希望这些反斜杠做什么?用下面的格式6/10/60将日、月和年分开如果你想打印正斜杠,我无法想象你为什么在你的问题中加反斜杠。。。但是好的,这就解释了你想做什么。你希望这些反斜杠做什么?用下面的格式6/10/60分开日、月和年如果你想打印前斜杠,我无法想象你为什么在你的问题中加反斜杠。。。但好吧,这就解释了你想做什么。非常感谢你。我甚至没想到。我试过其他的组合。非常感谢你。我甚至没想到。我尝试过其他所有组合。@Nick:
print(r'{}\{}\{}}是一个神奇的日期)。格式化(日、月、年))
用于反斜杠(例如,在Windows路径中)。两种日期格式都不明确
YYYY-MM-DD
可能是一个更好的选择。@Nick:
print(r'{}\{}\{}}是一个神奇的日期)。格式(日、月、年))
用于反斜杠(例如,在Windows路径中)。两种日期格式都不明确<代码>YYYY-MM-DD可能是更好的选择。