0
\n in Python
I would like to ask what do I need to write if I wanted the output to be : Hello World >>> I know I could write this using print('Hello\n\n\n\n','World'), but is there a way I could just do something like \n*3 or anything else to cut the code shorter?
5 Respuestas
+ 3
Sorry, it didn't work, because you can't use backslash \ in f-string's curly brackets, but you could store newline in a variable:
nline = "\n"
And then put it in f-string:
f"Hello {nline*5} World" ---> "Hello \n\n\n\n\n World"
+ 1
You can use fstrings, which let you perform evaluations in strings.
Start the string with an f:
f""
And put the evaluatable parts in {}:
f"Hello {5 + 5} World" ---> "Hello 10 World"
f"Hello {'\n'*3} World" ---> "Hello \n\n\n World"
+ 1
Seb TheS i havent learnt fstrings yet, but I've tried the code, my output comes out as empty though :'(
+ 1
Mirielle[ InAcTiVe ] thanks a lot! ✨