f-string backslash needs escape - sometimes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

f-string backslash needs escape - sometimes

OK Pythonistas, why does the backslash in line 5 need an escape backslash when the backslashes in lines 4, 6 and 7 don't? It's not a Code Playground quirk as the code works the same in Pydroid3. I know you can't put a backslash inside an f-string {} expression - hence the use of chr() - but this is weird 🤔 https://code.sololearn.com/c6sWEp4221jX/?ref=app

1st Jan 2019, 11:05 PM
David Ashton
David Ashton - avatar
7 Answers
+ 9
I think the answer is that otherwise the backslash would escape the quotation mark and the f-string would carry on into the next line.
1st Jan 2019, 11:33 PM
David Ashton
David Ashton - avatar
+ 1
David Ashton, Thank you so much I will check them 👍😁
3rd Jan 2019, 12:28 PM
Edwing123
Edwing123 - avatar
0
What does f-string do in Pyrhon?
2nd Jan 2019, 1:36 PM
Edwing123
Edwing123 - avatar
0
F-string what did string ever do to you. Ba-dum-bum.
2nd Jan 2019, 5:17 PM
ty_willingham
0
F string basically is a way to include variables or some expression inside an output string s="house" print(f"My {s} was burnt by them") Output: My house were burnt by them this is basically a faster and more intuitive way of implementing the same logic achieved by using + to concat during output or using the %d %s and such in some other languages
20th Mar 2019, 2:39 AM
Ritwik Banerjee
- 2
Can someone please and this >>>' '
2nd Jan 2019, 6:11 PM
Newman Richard
Newman Richard - avatar