Printing a variable


does every kind of a message before being printed have to be surrounded by quotes, or no? I’m interested especially in variables.

Thank you.

The only time we use quotes is when we are declaring a string literal.

a_string = "A string literal in double quotes"

b_string = 'A string literal in single quotes'

print (a_string)    # A string literal in double quotes

print (b_string)    # A string literal in single quotes

Notice the quotes are not printed, only the string data. Note also that we do not wrap the variable in quotes, else it would become a string literal, itself, not a variable name.

When printing a string directly, it will need the quotes or it won’t be recognized as a string and will raise a syntax error or perhaps even an unexpected identifier.

print ("A string literal in double quotes")

# A string literal in double quotes

Thank you very much sir.

1 Like