Skill - raw strings in Python
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
- strings in python
Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision
while declaring a string, the \
character is treated as an escape character, hence to incorporate \
in our strings, we have to write \\
.
To avoid this we can use raw strings as shown below
Code
# without using raw strings
x = 'Hi\nSudhir\n'
print(x)
# This will print
# Hi
# Sudhir
# using raw strings
y = r'Hi\nSudhir\n'
print(y)
# This will print
# Hi\nSudhir\n
# Notice that \n is used as it is, since we used 'r' before string declaration
# without using raw strings
z = 'C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard'
print(z)
# This will print
# 'C:\Users\Nagasudhir\Documents\Static_Web_Projects\WRLDC Apps Dashboard'
# Notice that we had to use \\ in order to print \
# using raw strings
k = r'C:\Users\Nagasudhir\Documents\Static_Web_Projects\WRLDC Apps Dashboard'
print(k)
# This will print
# 'C:\Users\Nagasudhir\Documents\Static_Web_Projects\WRLDC Apps Dashboard'
# Notice that we need not use \\ since we used 'r' before string declaration
Online Interpreter
You can run these codes online at https://www.programiz.com/python-programming/online-compiler/
Comments
Post a Comment