Convert String to Raw String in Python

Raw strings provide a useful way to describe strings in Python so that the backslashes and their succeeding characters are not interpreted as escape sequences. The interpreter does not implement the escape sequence in such strings but rather considers them as normal characters.

This tutorial will demonstrate how to convert string to raw string in Python.

Convert String to Raw String in Python

Strings are immutable objects which means that we cannot modify them. In the methods discussed below, converting string to raw string indicates that we will create a raw string from the existing string.

Using the r Prefix to Convert String to Raw String in Python

As discussed earlier, raw strings are just a different way to describe strings in Python. We can create such raw strings by adding the prefix r before the string declaration.

This is the syntax that conveys to the Python interpreter to create a raw string.

For example,

Output:

java\n2\nblog

Using the repr() Function to Convert String to Raw String in Python

The repr() function is used to return the actual string representation of a given object. It is similar to the str() function, but the str() function returns the printable string representation.

We can use it to convert string to raw string in Python.

For example,

Output:

‘java\n2\nblog’

Using the encode() and decode() Functions to Convert String to Raw String in Python

Python 3 treats a string as a collection of Unicode characters. We can use the encode() function is used to encode the string to the specified encoding and similarly the decode() function decodes the encoded string.

We can use these functions one after the other to convert string to raw string in Python. First, we will encode the string to the unicode_escape encoding which will ensure that the backslashes are not escaped, and then decode it to preserve the escape sequences like a raw string.

See the code below.

Output:

java\n2\nblog

Using String Formatting to Convert String to Raw String in Python

Python provides us with several methods to format a string so that we can get the output in our desired style. One such method includes the use of the format() function and fstrings. We cannot use them directly to convert string to raw string.

However, we can use them if we wish to interpolate a string and add some missing variables to it with these methods and want to ensure that the final result is described as a raw string.

We will prefix r before using them to achieve the same.

Output:

java\n2\nblog
java\n2\nblog

Conclusion

To conclude, we discussed several methods to convert string to raw string in Python. First, we discussed the basics of raw strings and how we create them by using the r prefix before string declaration. We discussed the use of the repr() function to achieve the same. We also demonstrated the use of encode() and decode() functions in succession to convert string to raw string in Python. String formatting was also discussed to achieve the same to some extent while interpolation.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *