How to include a double-quote and/or single-quote character in a raw Python string literal?

If you want to use double quotes in strings but not single quotes, you can just use single quotes as the delimiter instead: r’what”ever’ If you need both kinds of quotes in your string, use a triple-quoted string: r”””what”ev’er””” If you want to include both kinds of triple-quoted strings in your string (an extremely unlikely … Read more

How can I put an actual backslash in a string literal (not use it for an escape sequence)?

To answer your question directly, put r in front of the string. final= path + r’\xulrunner.exe ‘ + path + r’\application.ini’ But a better solution would be os.path.join: final = os.path.join(path, ‘xulrunner.exe’) + ‘ ‘ + \ os.path.join(path, ‘application.ini’) (the backslash there is escaping a newline, but you could put the whole thing on one … Read more

Python Regex escape operator \ in substitutions & raw strings

First and foremost, replacement patterns ≠ regular expression patterns We use a regex pattern to search for matches, we use replacement patterns to replace matches found with regex. NOTE: The only special character in a substitution pattern is a backslash, \. Only the backslash must be doubled. Replacement pattern syntax in Python The re.sub docs … Read more

What is a raw string?

Raw string literals are string literals that are designed to make it easier to include nested characters like quotation marks and backslashes that normally have meanings as delimiters and escape sequence starts. They’re useful for, say, encoding text like HTML. For example, contrast “<a href=\”file\”>C:\\Program Files\\</a>” which is a regular string literal, with R”(<a href=”https://stackoverflow.com/questions/56710024/file”>C:\Program … Read more

What exactly do “u” and “r” string prefixes do, and what are raw string literals?

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 … Read more