• \r = CR (Carriage Return) β†’ Used as a new line character in Mac OS before X
  • \n = LF (Line Feed) β†’ Used as a new line character in Unix/Mac OS X
  • \r\n = CR + LF β†’ Used as a new line character in Windows
Answer from Tom Bowen on Stack Overflow
🌐
W3Schools
w3schools.com β€Ί r β€Ί r_strings.asp
R Strings / Characters
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Variables Concatenate Elements Multiple Variables Variable Names R Data Types R Numbers R Math R Strings Β· Strings Escape Characters R Booleans R Operators R If...Else Β· If...Else Nested If And Or R While Loop R For Loop ... Strings are used for storing text. A string is surrounded by either single quotation marks, or double quotation marks:
🌐
PacketPushers
packetpushers.net β€Ί home β€Ί what does an β€˜r’ before a string mean in python?
What Does An 'R' Before A String Mean In Python?
January 26, 2024 - An β€˜r’ before a string tells the Python interpreter to treat backslashes as a literal (raw) character. Normally, Python uses backslashes as escape characters. Prefacing the string definition with β€˜r’ is a useful way to define a string ...
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί r'', regular expressions, raw strings and the \
r'', regular expressions, raw strings and the \ : r/learnpython
March 9, 2025 -

I've just completed the freecodecamp python password generator sequence of lessons and I am a bit stumped when it comes to the prefix r''. I understand that it is called a raw string, and that it is used so that python does not treat \ as an escape character. That makes sense, but learning about r'', regex and everything in the sequence of lessons has left me quite confused on a few points so I hope you don't mind me listing them here:

  1. can r'' be used for anything other than making \ a normal character?

  2. is the r in r'' the same as the r in re.?

  3. In the project, there are lots of instances of r'[A-Z]' and r'[a-z]' etc, uses of r'' without a proceeding \, what is the point of including the r'' if there is no backslash?!

Feeling a little lost on this one, if anyone can point me towards any resources where I can explore these concepts further that would be amazing.

Top answer
1 of 3
4
https://www.geeksforgeeks.org/python-raw-strings/ No, nothing to do with regex People are used to it? Idk I'll always use a raw string with regex
2 of 3
2
the r prefix is just part of the string literal syntax, and not anything to do with regex. It stands for 'raw' string. r-strings produce normal str objects just like any other string, and any string you can write as an r-string you can write as a normal not-r-string. Normally, when you write a string literal, Python turns a pair of backslash+character into magic escape codes, i.e. \n becomes a newline, \t becomes a tab, \\ becomes a single literal backslash, and so on. The only thing r-strings do is disable this. So r'a\nb' is the same as the normal string 'a\\nb'. This is particularly useful for regexes, because they often have literal backslashes in them (which then mean something to the regex engine), so you often want to disable the magic escaping normal string literals do when using regexes. Since they're not anything to do with regexes, and are just purely something about the syntax, the official Python docs covers them well https://docs.python.org/3/reference/lexical_analysis.html#literals If there are no backslashes in the string, an r-prefix on the literal does not make a difference. It could be that they either misunderstood and thought the 'r' was something about regexes, or did it as a habit or style choice (to make all regex arguments be raw strings to avoid easy to make bugs), or their IDE does regex syntax highlighting on all raw strings or something.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί r language β€Ί r-strings
R Strings - GeeksforGeeks
June 7, 2025 - Data Viz. using R ... Strings are a bunch of character variables. It is a one-dimensional array of characters. One or more characters enclosed in a pair of matching single or double quotes can be considered a string in R.
🌐
Quora
quora.com β€Ί What-does-r-mean-in-a-programming-language
What does \r mean in a programming language? - Quora
In most computer languages β€œ\r” represents a β€œcarriage return”. The two characters when entered into a computer program will be replaced by a single character with the ASCII code of 13.
🌐
TutorialsPoint
tutorialspoint.com β€Ί r β€Ί r_strings.htm
R - Strings
Any value written within a pair of single quote or double quotes in R is treated as a string. Internally R stores every string within double quotes, even when you create them with single quote.
🌐
Programiz
programiz.com β€Ί r β€Ί strings
R Strings (with Examples)
Since strings are represented by double quotes, the compiler will treat "This is " as the string. Hence, the above code will cause an error. To solve this issue, we use the escape character \ in R.
Find elsewhere
🌐
R for Data Science
r4ds.hadley.nz β€Ί strings.html
14 Strings – R for Data Science (2e)
Raw strings are flexible enough to handle any text. As well as ", ', and \, there are a handful of other special characters that may come in handy. The most common are \n, a new line, and \t, tab. You’ll also sometimes see strings containing Unicode escapes that start with \u or \U. This is a way of writing non-English characters that work on all systems.
🌐
DataCamp
datacamp.com β€Ί tutorial β€Ί strings-in-r
Strings in R Tutorial | DataCamp
April 6, 2020 - Learn about R's Strings: its rules, concatenation, along with essential properties of the string, extracting and replacing a character string, and formatting a string. ... Get your team access to the full DataCamp for business platform. A string is a character that is made of one character or contains a collection of characters. It is enclosed inside single quotes('This is a string') or inside the double quotes("This is also a string").
🌐
Codecademy
codecademy.com β€Ί docs β€Ί r β€Ί strings
R | Strings | Codecademy
May 31, 2023 - Strings in R are a fundamental data type used for storing text data. They are created using quotation marks, either single (β€˜) or double (β€œ), and can contain any combination of letters, numbers, and symbols.
🌐
Supersourcing
supersourcing.com β€Ί home β€Ί tech resources β€Ί what is r in python? what is its purpose?
What is R in Python? What is its purpose? - Supersourcing
July 18, 2025 - In Python, the letter β€˜r’ preceding a string signifies a β€˜Raw String’. This instructs the Python interpreter to interpret backslashes in the string literally, rather than as escape characters, which is the default behavior.
🌐
Quora
quora.com β€Ί What-does-an-R-mean-before-a-string-in-Python
What does an R mean before a string in Python? - Quora
Answer (1 of 2): In python a string that is prefixed with an [code ]r[/code] before the opening quotes is a β€œraw” or sometimes a regex string. In a normal string character pairs and groups that start with a backslash [code ]\[/code] are ...
🌐
Gastonsanchez
gastonsanchez.com β€Ί r4strings β€Ί chars.html
2 Character Strings in R | Handling Strings With R
This chapter introduces you to the basic concepts for creating character vectors and character strings in R. You will also learn how R treats objects containing characters. In R, a piece of text is represented as a sequence of characters (letters, numbers, and symbols).
🌐
IONOS
ionos.com β€Ί digital guide β€Ί websites β€Ί web development β€Ί r strings
How to create and use strings in R
November 10, 2023 - Strings are a fundamental data structure in R. They are used to display sequences of characters and individual letters. In contrast to other programming languages, R does not have a data type called β€œstring”. Instead, this R data type is ...
Top answer
1 of 5
73

\r is "Carriage Return" (CR, ASCII character 13), \n is "Line Feed" (LF, ASCII character 10). Back in the days, you had two ASCII characters at the end of each line to tell a printer what to do - CR would tell the printer to go back to the left edge of the paper, LF would advance to the next line.

Operating systems still have different conventions as to what the end of a line looks like -- some of them have \n\r, some have \n, some have \r\n.

In Javascript, you mostly deal with \n - this is how strings are typically switching to the next line. However, depending on what strings you are working with, you may be encountering \r as well.

2 of 5
26

Normally \r represents a carriage return character (ASCII 0x0d), and \n is a newline character (ASCII 0x0a). This page has a list of all the special characters, quoted here for completeness:

  • \f matches form-feed.
  • \r matches carriage return.
  • \n matches linefeed.
  • \t matches horizontal tab.
  • \v matches vertical tab.
  • \0 matches NUL character.
  • [\b] matches backspace.
  • \s matches whitespace (short for [\f\n\r\t\v\u00A0\u2028\u2029]).
  • \S matches anything but a whitespace (short for [^\f\n\r\t\v\u00A0\u2028\u2029]).
  • \w matches any alphanumerical character (word characters) including underscore (short for [a-zA-Z0-9_]).
  • \W matches any non-word characters (short for [^a-zA-Z0-9_]).
  • \d matches any digit (short for [0-9]).
  • \D matches any non-digit (short for [^0-9]).
  • \b matches a word boundary (the position between a word and a space).
  • \B matches a non-word boundary (short for [^\b]).
  • \cX matches a control character. E.g: \cm matches control-M.
  • \xhh matches the character with two characters of hexadecimal code hh.
  • \uhhhh matches the Unicode character with four characters of hexadecimal code hhhh.
Top answer
1 of 8
927

There's not really any "raw string"; there are raw string literals, which are exactly the string literals marked by an r before the opening quote.

A "raw string literal" is a slightly different syntax for a string literal, in which a backslash, \, is taken as meaning "just a backslash" (except when it comes right before a quote that would otherwise terminate the literal) -- no "escape sequences" to represent newlines, tabs, backspaces, form-feeds, and so on. In normal string literals, each backslash must be doubled up to avoid being taken as the start of an escape sequence.

This syntax variant exists mostly because the syntax of regular expression patterns is heavy with backslashes (but never at the end, so the "except" clause above doesn't matter) and it looks a bit better when you avoid doubling up each of them -- that's all. It also gained some popularity to express native Windows file paths (with backslashes instead of regular slashes like on other platforms), but that's very rarely needed (since normal slashes mostly work fine on Windows too) and imperfect (due to the "except" clause above).

r'...' is a byte string (in Python 2.*), ur'...' is a Unicode string (again, in Python 2.*), and any of the other three kinds of quoting also produces exactly the same types of strings (so for example r'...', r'''...''', r"...", r"""...""" are all byte strings, and so on).

Not sure what you mean by "going back" - there is no intrinsically back and forward directions, because there's no raw string type, it's just an alternative syntax to express perfectly normal string objects, byte or unicode as they may be.

And yes, in Python 2.*, u'...' is of course always distinct from just '...' -- the former is a unicode string, the latter is a byte string. What encoding the literal might be expressed in is a completely orthogonal issue.

E.g., consider (Python 2.6):

>>> sys.getsizeof('ciao')
28
>>> sys.getsizeof(u'ciao')
34

The Unicode object of course takes more memory space (very small difference for a very short string, obviously ;-).

2 of 8
231

There are two types of string in Python 2: the traditional str type and the newer unicode type. If you type a string literal without the u in front you get the old str type which stores 8-bit characters, and with the u in front you get the newer unicode type that can store any Unicode character.

The r doesn't change the type at all, it just changes how the string literal is interpreted. Without the r, backslashes are treated as escape characters. With the r, backslashes are treated as literal. Either way, the type is the same.

ur is of course a Unicode string where backslashes are literal backslashes, not part of escape codes.

You can try to convert a Unicode string to an old string using the str() function, but if there are any unicode characters that cannot be represented in the old string, you will get an exception. You could replace them with question marks first if you wish, but of course this would cause those characters to be unreadable. It is not recommended to use the str type if you want to correctly handle unicode characters.

🌐
Sololearn
sololearn.com β€Ί en β€Ί Discuss β€Ί 1636326 β€Ί what-does-the-r-mean-in-strings
what does the '\r' mean in strings? | Sololearn: Learn to code for FREE!
I understood the splitlines method ... >>> ['ab c', '', 'de fg', 'kl'] ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] ... \r is a carriage return whereas \n is a new line (imagine an old typewriter machine changes line in two steps; ...
🌐
W3docs
w3docs.com β€Ί python
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
In Python, the "r" prefix before a string denotes that it is a raw string literal. This means that any backslashes () in the string are not treated as escape characters.