110

I have a string s, its contents are variable. How can I make it a raw string? I'm looking for something similar to the r'' method.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
  • 10
    Raw strings are just a different syntax when defining a string constant. What is it in particular you want from var that makes you think of using raw strings? – Josh Bleecher Snyder Dec 11 '10 at 04:53
  • 1
    Are you using Python 2 or Python 3? If Python 3, are you perhaps asking about the `bytes` type? – Greg Hewgill Dec 11 '10 at 05:06
  • The question is not coherent. `r''` is not a "method", there is not actually such a thing as a "raw string" (there are only raw string *literals*, which are a different way of **describing** a string - and such a string is a **perfectly ordinary** string). It's not at all clear what transformation OP had in mind, or what purpose would have been served. Voting to close, as I should have instead of answering 12 years ago. – Karl Knechtel Jul 31 '22 at 04:44
  • 1
    See also https://stackoverflow.com/questions/21605526 – Karl Knechtel Jul 31 '22 at 04:59
  • 1
    See also https://stackoverflow.com/questions/2081640 – Karl Knechtel Aug 05 '22 at 04:16

12 Answers12

90

i believe what you're looking for is the str.encode("string-escape") function. For example, if you have a variable that you want to 'raw string':

a = '\x89'
a.encode('unicode_escape')
'\\x89'

Note: Use string-escape for python 2.x and older versions

I was searching for a similar solution and found the solution via: casting raw strings python

barley
  • 176
  • 1
  • 10
Jolly1234
  • 1,577
  • 12
  • 9
  • 3
    this is the solution. – erikbstack Nov 13 '13 at 13:16
  • 8
    On python 3.5.1: `LookupError: unknown encoding: string-escape` – nnyby Jan 03 '16 at 18:07
  • 1
    This "fails" for input `"an uppercase a is \x41"` – wim Aug 16 '19 at 18:17
  • 3
    To me it seems like "the r'' operator" and .encode() are not the same. These three: '\bla\ \n' --- r'\bla\ \n' --- ('\bla\ \n').encode("unicode_escape").decode() all give different strings it seems: '\x08la\\ \n' --- '\\bla\\ \\n' --- '\\x08la\\\\ \\n' – dasWesen Feb 01 '20 at 15:16
  • 7
    In case it also helps someone else, I also needed the additional `.decode()` at the end like at [the referenced source](https://stackoverflow.com/a/2428132/8508004) to get something like I was getting from `r"string_goes_here"` solely. However, it was a rather complex case where I was replicating an issue like [here](https://stackoverflow.com/q/60640682/8508004) and solving. – Wayne Mar 12 '20 at 17:11
  • Fails for `"\x22".encode('unicode_escape')`, in python that returns `b'"'` – John Sep 19 '22 at 19:47
84

Raw strings are not a different kind of string. They are a different way of describing a string in your source code. Once the string is created, it is what it is.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 35
    +1 for the rare, accurate usage of "it is what it is" – LoveMeSomeCode Apr 10 '14 at 15:38
  • 36
    this is actually wrong. There's another answer here with the correct response: "raw strings do not escape anything inside of them". – igorsantos07 Nov 08 '17 at 19:51
  • 3
    @igorsantos07 No, you are confused. When you *create* a string you may need to escape something; but once the string contains what it contains, "escaping it" is not a well-defined operation (though of course you can create a string with *different* contents by, for example, interpreting literal backslashes as escape codes). – tripleee Jul 08 '21 at 17:48
42

Since strings in Python are immutable, you cannot "make it" anything different. You can however, create a new raw string from s, like this:

raw_s = r'{}'.format(s)

slashCoder
  • 1,447
  • 21
  • 22
  • 3
    `>>> raw_s = r'{}'.format(normal) >>> raw_s 'The\n' >>> normal 'The\n' >>> raw=r"The\n" >>> raw 'The\\n'` does not provide same output as raw – N N K Teja Jun 10 '20 at 17:48
  • 11
    This does nothing. `r'{}'.format('\n') == '\n'`. The `r` prefix only applies to what's inside the string literal, i.e. the braces. – wjandrea Jul 09 '20 at 03:32
  • For Windows paths, be sure to use `os.path.join` if converting path strings to avoid trouble with so-called "escape characters" (a.k.a. backslashes). – brethvoice Feb 18 '21 at 14:34
  • 1
    This is roughly as useful as `str(str(str(str(s))))`; using `format` to put a string inside another string is just wasteful if one of them is otherwise empty. – tripleee Jul 08 '21 at 17:51
  • Clever, thanks. That's exactly what I was looking for and I've probably even used it in that way, but when we get stuck or a brain fart, there's Google. Thanks! – jacktrader Jul 22 '22 at 05:28
  • `r'{}'.format('\n') == r'\n'` return `False` – kirogasa Oct 22 '22 at 12:46
25

As of Python 3.6, you can use the following (similar to @slashCoder):

def to_raw(string):
    return fr"{string}"

my_dir ="C:\data\projects"
to_raw(my_dir)

yields 'C:\\data\\projects'. I'm using it on a Windows 10 machine to pass directories to functions.

dheinz
  • 988
  • 10
  • 16
  • 2
    `>>> def to_raw(string): ... return fr"{string}" ... >>> normal 'The\n' >>> to_raw(normal) 'The\n' >>> raw 'The\\n'` does not provide same output as raw – N N K Teja Jun 10 '20 at 17:49
  • 10
    This doesn't actually do anything. `my_dir` is already `'C:\\data\\projects'` because `\d` and `\p` are unrecognized escape sequences, so the backslashes are preserved. [Unrecognized escape sequences will raise a `SyntaxError` in a future version of Python](https://docs.python.org/3/reference/lexical_analysis.html#index-23). Also try `my_dir = 'C:\Users'`, which immediately raises a `SyntaxError`. – wjandrea Jul 08 '20 at 21:54
  • This is spot on - I get a file path using a TkInter dialog box, sending it to a string, which I then want to convert to a raw string to re-open the file. I can't therefore just add ```r'string'``` but ```fr"{string}"``` works perfectly! – ChemEnger Sep 09 '20 at 08:52
  • 2
    @ChemEnger `return string` would have worked just as well; `fr"{string}" == string` in all cases where `string` is an actual string and not for example an `int`. – Mark Ransom Jul 08 '21 at 17:44
  • Then not doing any of this is also spot on, because this doesn't actually do anything. – tripleee Jul 08 '21 at 18:00
  • 1
    What does the f stand for in fr"string"? Is it documented somewhere in Python Docs? I see the r"", but not fr"" – Patrick Woo Sep 08 '21 at 17:33
  • 1
    The f-string is a new string format introduced with Python 3.6 which eases putting a variable in a string representation. For instance, you have the variable `name` and want to put it into a print statement, you can achieve this by: `print(f"My name is {name}")`. In older version you could use the `format` statement as follows: `print("my name is {}".format(name))` See https://realpython.com/python-f-strings/ for some more documentation – dheinz Sep 08 '21 at 17:50
21

raw strings apply only to string literals. they exist so that you can more conveniently express strings that would be modified by escape sequence processing. This is most especially useful when writing out regular expressions, or other forms of code in string literals. if you want a unicode string without escape processing, just prefix it with ur, like ur'somestring'.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • 6
    I wouldn't expect the @TokenMacGuy to know this but they're also useful for defining paths on Windows which uses the backslash as a separator character in paths, such as `r'C:\Python27\Tools\Scripts\2to3.py'` – martineau Dec 11 '10 at 09:38
  • 8
    Alas, TokenMacGuy is just the name. My main machine runs windows. the *real* reason I don't use raw strings for filepaths is because I never hardcode pathnames. – SingleNegationElimination Dec 11 '10 at 15:27
5

For Python 3, the way to do this that doesn't add double backslashes and simply preserves \n, \t, etc. is:

a = 'hello\nbobby\nsally\n'
a.encode('unicode-escape').decode().replace('\\\\', '\\')
print(a)

Which gives a value that can be written as CSV:

hello\nbobby\nsally\n

There doesn't seem to be a solution for other special characters, however, that may get a single \ before them. It's a bummer. Solving that would be complex.

For example, to serialize a pandas.Series containing a list of strings with special characters in to a textfile in the format BERT expects with a CR between each sentence and a blank line between each document:

with open('sentences.csv', 'w') as f:

    current_idx = 0
    for idx, doc in sentences.items():
        # Insert a newline to separate documents
        if idx != current_idx:
            f.write('\n')
        # Write each sentence exactly as it appared to one line each
        for sentence in doc:
            f.write(sentence.encode('unicode-escape').decode().replace('\\\\', '\\') + '\n')

This outputs (for the Github CodeSearchNet docstrings for all languages tokenized into sentences):

Makes sure the fast-path emits in order.
@param value the value to emit or queue up\n@param delayError if true, errors are delayed until the source has terminated\n@param disposable the resource to dispose if the drain terminates

Mirrors the one ObservableSource in an Iterable of several ObservableSources that first either emits an item or sends\na termination notification.
Scheduler:\n{@code amb} does not operate by default on a particular {@link Scheduler}.
@param  the common element type\n@param sources\nan Iterable of ObservableSource sources competing to react first.
A subscription to each source will\noccur in the same order as in the Iterable.
@return an Observable that emits the same sequence as whichever of the source ObservableSources first\nemitted an item or sent a termination notification\n@see ReactiveX operators documentation: Amb


...
rjurney
  • 4,824
  • 5
  • 41
  • 62
5

Just format like that:

s = "your string"; raw_s = r'{0}'.format(s)

user14229198
  • 83
  • 1
  • 2
3
s = "hel\nlo"
raws = '%r'%s #coversion to raw string
#print(raws) will print 'hel\nlo' with single quotes.
print(raws[1:-1]) # will print hel\nlo without single quotes.
#raws[1:-1] string slicing is performed
b.s. Mohan
  • 39
  • 2
2

The solution, which worked for me was:

fr"{orignal_string}"

Suggested in comments by @ChemEnger

Michael D
  • 1,711
  • 4
  • 23
  • 38
1

I suppose repr function can help you:

s = 't\n'
repr(s)
"'t\\n'"
repr(s)[1:-1]
't\\n'
1

With a little bit correcting @Jolly1234's Answer: here is the code:

raw_string=path.encode('unicode_escape').decode()
Hacker6914
  • 247
  • 1
  • 9
  • doesn't work for `"\\ftac\admin\rec\pir".encode('unicode_escape').decode()` – Matt Nov 25 '22 at 15:03
  • does not work at all in any way imaginable, only makes it worse by adding additional backslashes, completely useless – DanDan Mar 21 '23 at 18:51
-3

Just simply use the encode function.

my_var = 'hello'
my_var_bytes = my_var.encode()
print(my_var_bytes)

And then to convert it back to a regular string do this

my_var_bytes = 'hello'
my_var = my_var_bytes.decode()
print(my_var)

--EDIT--

The following does not make the string raw but instead encodes it to bytes and decodes it.

MusicMan24
  • 51
  • 1
  • 10
  • 1
    `str.encode` encodes the string to `bytes`, it doesn't create a [raw string](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals), which is a string in which a backslash is treated literally, not as an escape character. – snakecharmerb Aug 16 '20 at 16:11
  • Sorry about that I got slightly confused between them. – MusicMan24 Aug 17 '20 at 17:07