Consider the following code snippet:
name1 = "Nadya"
name2 = "Jim"
def print_string():
string = f"{name1}\n\
{name2}"
print(string)
print_string()
which produces
Nadya
Jim
This works, but the 'break' in indentation on the second line of the string
definition looks ugly. I've found that if I indent the {name2}
line, this indentation shows up in the final string.
I'm trying to find a way to continue the f-string on a new line and indent it without the indentation showing up in the final string. Following something similar I've seen for ordinary strings, I've tried
name1 = "Nadya"
name2 = "Jim"
def print_string():
string = f"{name1}\n"
f"{name2}"
print(string)
print_string()
but this leads to an IndentationError: unexpected indent
. Is what I am trying possible in another way?
string = f"{name1}\n" \ # line continuation character
f"{name2}"