I noticed that running an f-string from the Python console with escape characters acts differently when not wrapped in a print() function. For example, wrapped in a print() function, \n
acts as expected:
>>>a = 1
>>>b = 'frog'
>>>c = 3
>>>print(f"""The first variable is {a}\nThe second is {b} and the third is {c}""")
The first variable is 1
The second is frog and the third is 3
Run directly, it acts differently, printing the \n
as text.
>>>f"""The first variable is {a}\nThe second is {b} and the third is {c}"""
'The first variable is 1\nThe second is frog and the third is 3'
My question is: What is the root cause of this change in behavior and is there anything one might need to be aware of when using such a formatted string in other instances - such as in queries, writing to text files, etc.?