\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
\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
All 3 of them represent the end of a line. But...
\r(Carriage Return) β moves the cursor to the beginning of the line without advancing to the next line\n(Line Feed) β moves the cursor down to the next line without returning to the beginning of the line β In a *nix environment\nmoves to the beginning of the line.\r\n(End Of Line) β a combination of\rand\n
Videos
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:
can r'' be used for anything other than making \ a normal character?
is the r in r'' the same as the r in re.?
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.
\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.
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:
\fmatches form-feed.\rmatches carriage return.\nmatches linefeed.\tmatches horizontal tab.\vmatches vertical tab.\0matchesNULcharacter.[\b]matches backspace.\smatches whitespace (short for[\f\n\r\t\v\u00A0\u2028\u2029]).\Smatches anything but a whitespace (short for[^\f\n\r\t\v\u00A0\u2028\u2029]).\wmatches any alphanumerical character (word characters) including underscore (short for[a-zA-Z0-9_]).\Wmatches any non-word characters (short for[^a-zA-Z0-9_]).\dmatches any digit (short for[0-9]).\Dmatches any non-digit (short for[^0-9]).\bmatches a word boundary (the position between a word and a space).\Bmatches a non-word boundary (short for[^\b]).\cXmatches a control character. E.g:\cmmatchescontrol-M.\xhhmatches the character with two characters of hexadecimal codehh.\uhhhhmatches the Unicode character with four characters of hexadecimal codehhhh.
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 ;-).
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.