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?

21st Sep 2020, 1:33 PM
Shiau Huei Chang
Shiau Huei Chang - avatar
5 Respostas
+ 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"
21st Sep 2020, 2:02 PM
Seb TheS
Seb TheS - avatar
+ 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"
21st Sep 2020, 1:52 PM
Seb TheS
Seb TheS - avatar
+ 1
Seb TheS i havent learnt fstrings yet, but I've tried the code, my output comes out as empty though :'(
21st Sep 2020, 2:28 PM
Shiau Huei Chang
Shiau Huei Chang - avatar
+ 1
Mirielle[ InAcTiVe ] thanks a lot! ✨
21st Sep 2020, 2:29 PM
Shiau Huei Chang
Shiau Huei Chang - avatar