2012-10-07 44 views
0

我對這個問題的處理很困難。每次我嘗試在這個程序中反斜槓,它只會給我一個錯誤。「行」之前的反斜槓字符 - Python3中的打印功能

我想該行

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

但無論我怎麼經常嘗試別的東西,似乎沒有任何工作。任何建議都會非常有幫助。

+1

你會期望那些反斜槓做什麼? – stranac

+0

用以下格式分隔一天中的月份和年份6/10/60 – Nick

+2

如果您想打印正斜槓,我無法想象爲什麼在您的問題中放置反斜槓......但好的,這解釋了您想要執行的操作。 – stranac

回答

0

你需要告訴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 
+0

哦。非常感謝你。我甚至沒有想到這一點。我曾嘗試過其他任何組合。 – Nick

3

由於Oz123已經寫了什麼的替代方案,你可能想要使用字符串格式:

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

非常感謝。 – Nick

+1

@Nick:對於反斜線(例如,在Windows路徑中),print(r'{} \ {} \ {}是一個神奇的'date'.format(day,month,year))'。兩種日期格式都不明確。 'YYYY-MM-DD'可能是更好的選擇。 – jfs