r before the string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

r before the string

Why print(r'\nHello\nWorld') its output would be \nHello\nWorld, where is the r in the beginning ? What does it mean?

6th Mar 2021, 9:33 AM
Boubaker Benaissa
Boubaker Benaissa - avatar
7 Answers
+ 12
For strings, there are several codes you can use to do a certain thing. '\n' means newline, so when you write ... print('Hello\nworld') ... it is printed in two lines. Having the r before the string (standing for 'raw') is an order to ignore all these codes and just print everything as it is. Basically it enters an extra '\' in front of each of these codes which will lead to them being ignored. You could also do that by hand, writing a bunch of '\' in front of all the codes.
6th Mar 2021, 9:38 AM
HonFu
HonFu - avatar
+ 1
Thank you, i understand now
6th Mar 2021, 9:41 AM
Boubaker Benaissa
Boubaker Benaissa - avatar
+ 1
Read Eve's answer in the thread below...he explained the concept very nicely https://www.sololearn.com/discuss/2696669/?ref=app
6th Mar 2021, 12:24 PM
∆BH∆Y
∆BH∆Y - avatar
+ 1
it will print: Hello 'whatevever the user inputs'!!!
7th Mar 2021, 11:34 AM
LoloXploit
LoloXploit - avatar
0
Hey if u wanna get creative, use only f strings like this
7th Mar 2021, 11:32 AM
LoloXploit
LoloXploit - avatar
0
name = input("What is your name: ")
7th Mar 2021, 11:32 AM
LoloXploit
LoloXploit - avatar
0
print(f"Hello {name}!!")
7th Mar 2021, 11:33 AM
LoloXploit
LoloXploit - avatar