Monday 20 September 2021

python: Escape \n (new line) character

There are two solutions to this problem.

 

Solution 1: Escaping the \n character.

str1 = "Hi, I am escaping \\n and it will print \\n"

 

Solution: Prefixing the string with character ‘r’.

str2 = r"Hi, I am escaping \n and it will print \n"

Find the below working application.

 

escape_new_line.py

str1 = "Hi, I am escaping \\n and it will print \\n"
str2 = r"Hi, I am escaping \n and it will print \n"

print('str1', ' -> ' , str1)
print('str2', ' -> ' , str2)

 

Output

str1  ->  Hi, I am escaping \n and it will print \n
str2  ->  Hi, I am escaping \n and it will print \n

 

 

 



Previous                                                    Next                                                    Home

No comments:

Post a Comment