Home > database >  python how to get today, variable with quote
python how to get today, variable with quote

Time:08-15

What I need is how to get today's date with quote sign, "2022-08-14".

The date can be changed on daily basis, and the quote sign, ", stayed at the begining and the end.

having to do with the sign, &, or not. I cant figured it out. Thanks in advance

from datetime import date
from datetime import timedelta
n=0
today = date.today() - timedelta(days = n)
print(today)
print("today")
print("", today , "")

This is what I got from above print

2022-08-14

today

2022-08-14

CodePudding user response:

You need to put the quotes in the string, e.g.

print('"', today, '"')

You could also use triple quotes:

print(""""""", today, """"""")

but that looks harder to read to me.

Your code is incorrect because you've got empty strings either side of today.

CodePudding user response:

In python, to print the double quote " as a string not a symbol, you can use \" inside the quotes like print("\"")

Or you can just use single quotes wrapping a double quote (print('"'))

In your case, I think the simplest solution is to use f-string where variable inside {} will be printed like string

print(f'"{today}"')

This is most basic grammar and I recommend you do some web search before asking here

  • Related