Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

While asking this question, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks.

I know what an encoding is, and I know what u'' alone does since I get what is Unicode.

  • But what does r'' do exactly? What kind of string does it result in?

  • And above all, what the heck does ur'' do?

  • Finally, is there any reliable way to go back from a Unicode string to a simple raw string?

  • Ah, and by the way, if your system and your text editor charset are set to UTF-8, does u''actually do anything?

1 Answer

0 votes
by (106k points)

In Python we have two types of string: 

The first one is the traditional str type and the second one is the newer Unicode type. When you type a string literal without u in front you get the old str type which stores 8-bit characters, and with u in front, you will get the newer Unicode type that can store any Unicode character.

Talking about r so it doesn't change the type at all, it just changes how the string literal is interpreted. Without using r, backslashes are treated as escape characters. With the r, backslashes are treated as literal. Either way, you can use both types are the same.

Related questions

Browse Categories

...