Python

What is R String in Python?

You may have heard and used many string variables within the programming language while coding. The Python R string i.e. Raw string is used as a prefixed lateral to consider some special characters i.e. backslash as a normal character or literal string. Within this article, we will see how R string can be used with strings to perform this specific task within the Ubuntu 20.04 system.

Example 1:

Start with the creation of a simple Python file. You can name it as you want with the touch query in the shell. Try to open it within the GNU Nano editor to write code in it.

$ touch rstring.py

$ nano rstring.py

We are starting from the example with no use of the “R” i.e. raw string in the python code. So, we have to add the python 3 support at the top of the Python file as highlighted in the red color within the image beneath. After the support has been added, we have initialized a string variable named “v”. This string contains many of the escape characters within it i.e. “\n” used to give a line gap after the words or characters. The print clause is used to print this variable. The script for this example is presented here.

#!/usr/bin/python3

v = “Example of \nR String \nin Python”

print(v)

The python3 keyword will be used to execute the python code file. So, we will do the same. After running the code file, we have got the string display in three lines as shown below. This happened, due to the usage of escape characters “\n” within the string.

$ python3 rstring.py

Let’s use the R string within our code to get an updated result. So, we have opened the same file and added the R string at the start of the initialization of a string value as you can see from the image below. The script for this example is presented here.

#!/usr/bin/python3

v = R“Example of \nR String \nin Python”

print(v)

After running the code file on the shell with the python3 keyword, we have got the string displayed in one line without any line jump. This is because the R string considers every escape character as a normal and literal character and prints it out as it is.

$ python3 rstring.py

Example 2:

In the above example, we have seen how the R string works on the “\n” escape character. Let’s see how it will work on the “\x”. As the “\x” is a raw string literal and cannot be encoded, thus we must find the exception. So, we have opened the same file and added python3 support at the very start.

The variable “v” has been initialized with a string value. This variable “v” contains the escape characters and raw string literals in it as well i.e. “\x”. The print statement is used to print out the variable “v” string value on the shell. The script for this example is presented here.

#!/usr/bin/python3

v = “Example of \xR String \nin Python”

print(v)

We have executed our python code with the help of a “python3” keyword package. In return, we have got the error showing that “\x” cannot be decoded because it’s not a proper escape character or its Unicode.

$ python3 rstring.py

After opening the file, we have added the R string before the initialization value of a variable string “v”. This will help us remove the above error and display the string as it is. The script for this example is presented here.

#!/usr/bin/python3

v = R“Example of \xR String \nin Python”

print(v)

After running the updated code on the shell with the help of a python3 package, we have got the string “v” displayed on the shell as it is without any change and error.

$ python3 rstring.py

Conclusion

This article contains two examples to illustrate the working of the R string in the Python code i.e. Raw string. These two examples show how the R string can ignore or simply consider the escape characters as normal literals within the Python code.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content