The raw string notation is only used in Python source code; all strings declared as raw strings are "converted" to normal strings with the necessary escape sequences added during "compile time" (unlike (in Python 2) the two different string types string/Unicode string):

>>> r"\b"
'\\b'
>>> "Hello"
'Hello' 
>>> u"Hello"
u'Hello'

If you read the string from a file, it will already be correctly escaped.

(Assuming test.txt contains (?i:\bsys\.user_catalog\b)):

f = open("test.txt").read()
print f
print repr(f)

Output:

(?i:\bsys\.user_catalog\b)
'(?i:\\bsys\\.user_catalog\\b)'
Answer from Tim Pietzcker on Stack Overflow
🌐
Python
mail.python.org › pipermail › tutor › 2004-April › 029022.html
[Tutor] can you turn strings from a file into raw strings?
September 9, 2013 - Otherwise, the directory separator '\' gets read as >>an escape character. I know I can solve it by using '/' in place of > > > Did you actually try that? Here's a small example: > > $ cat > raw_string > c:\foo\bar\test\new\end > > $ python > Python 2.3.3 (#1, Dec 30 2003, 08:29:25) > [GCC 3.3.1 (cygming special)] on cygwin > Type "help", "copyright", "credits" or "license" for more information. > >>>>f = file('raw_string') >>>>f.readline() > > 'c:\\foo\\bar\\test\\new\\end\r\n' > >>>>f = file('raw_string') >>>>print f.readline() > > c:\foo\bar\test\new\end > > No interpolation happens (\t is tab and \n newline).
Discussions

Save strings as raw string to txt file
You mean you want literal \ and n characters? That's not a raw string, it's an "escaped" string. You can do it in python with the "unicode-escape" encoding: y = ''' Hello, how are you ''' with open(filename, 'w', encoding='unicode-escape') as f: f.write(y) You would use same encoding in read mode or the ast.literal_eval function to convert it back. Or you can use the json module for both, which will do the same thing but it adds quotes on the outside. More on reddit.com
🌐 r/learnpython
4
1
February 14, 2022
binaryfiles - How to open a binary file as raw string in python? - Stack Overflow
Is there some easy way in python how to open a binary file as raw string? I am totally new to python. Thanks for your help in advance. ... After you have opened a file, that file reference has a read() method on it which takes the number of bytes that you want to read in. More on stackoverflow.com
🌐 stackoverflow.com
Python how to read raw binary from a file? (audio/video/text) - Stack Overflow
I want to read the raw binary of a file and put it into a string. Currently I am opening a file with the "rb" flag and printing the byte but it's coming up as ASCII characters (for text that is, for More on stackoverflow.com
🌐 stackoverflow.com
How to create raw string from string variable in python? - Stack Overflow
Traceback (most recent call last): ...ath),"r").readlines() IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\x08_zz\\Desktop\\my_file' ... Why not just write r"C:\Users\b_zz\Desktop\my_file" in the first place? Or better yet, "C:/Users/b_zz/Desktop/my_file"? ... There is no such thing as "raw string" once the ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Narkive
tutor.python.narkive.com › E1LcyYsP › can-you-turn-strings-from-a-file-into-raw-strings
[Tutor] can you turn strings from a file into raw strings?
I can read the file just fine and have can do all I've tried with the output of the readline() method so far. But, since I am on a Windows machine, I would prefer to turn the readline() output into a raw string. Otherwise, the directory separator '\' gets read as an escape character. I know I can solve it by using '/' in place of Did you actually try that? Here's a small example: $ cat > raw_string c:\foo\bar\test\new\end $ python ...
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › webprogramming › Python_FileText.html
Python Tutorial - File and Text Processing
You can optionally specify the ... positions at the beginning of the file and advances whenever read/write operations are performed. fileObj.readline() -> str: (most commonly-used) Read next line (up to and including newline) and return a string (including newli...
🌐
Reddit
reddit.com › r/learnpython › save strings as raw string to txt file
r/learnpython on Reddit: Save strings as raw string to txt file
February 14, 2022 -

I am trying to import documents of multi-paragraph text into Microsoft Access, after processing them in Python. Unfortunately, Access seem to think each sentence is a new record, despite setting "^" as the delimiter.

I want to instead write my strings in the raw string format to work around this problem.

So I want:

y = '''
Hello, how are
    you
'''

to be saved as '\nHello, how are\n you\n' in a txt file.

I cannot find anything on how to convert or encode strings to raw strings. How do I go about doing this?

And also, I want to be able to read '\nHello, how are\n you\n' from the text file and convert it back to a normal Python string. How do I go about doing this?

Find elsewhere
🌐
Finxter
blog.finxter.com › python-raw-strings-a-helpful-easy-guide
Python Raw Strings: A Helpful Easy Guide – Be on the Right Side of Change
March 26, 2023 - Backslashes are used as the path ... in Python strings. Using raw strings can simplify the process of working with these file paths. For example, consider the following Windows file path: ... Another common use case for raw strings is when working with regular expressions. 💡 Regular expressions frequently use backslashes as escape characters, which can make it difficult to read and write ...
🌐
Quora
quora.com › What-is-a-raw-string-in-Python
What is a raw string in Python? - Quora
Just sign up on freecash now and start earning from the first minute (yes, you’ve read that right). ... Raw strings are raw string literals that treat backslash (\) as a literal character. For example, if we try to print a string with a “\n” inside, it will add one line break. But if we mark it as a raw string, it will simply print out the “\n” as a normal character. Python raw strings are prefixed with ‘r’ or ‘R’. Prefix a string with ‘R’ or ‘r’ and it will be treated as a raw string.
🌐
AskPython
askpython.com › home › raw strings in python: a comprehensive guide
Raw Strings in Python: A Comprehensive Guide - AskPython
May 22, 2023 - Let’s examine the pros and cons of using raw strings in Python. Simplified Syntax: With raw strings, you can avoid the complications resulting from escape sequences, making it easier to deal with file paths, regular expressions, and other situations where special characters are common. Enhanced Readability...
🌐
GeeksforGeeks
geeksforgeeks.org › python-raw-strings
Python Raw Strings - GeeksforGeeks
# Python program to use raw strings # Define string with r before quotes # and assign to variable s1 = r'Python\nis\easy\to\learn' print(s1) ... In this example, we defined a string, which is basically a file path of Windows, using r before ...
Published   April 28, 2025
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-raw-string
How To Use Python Raw String | DigitalOcean
March 4, 2026 - Both patterns produce the same string, but the raw string version is easier to read and less error-prone. This is why Python’s official re module documentation recommends using raw strings for regex patterns. Windows uses backslashes as path separators, which can conflict with Python escape sequences: path = r'C:\Users\admin\new_project\test_file.txt'
🌐
GeeksforGeeks
geeksforgeeks.org › python › read-file-as-string-in-python
Read File As String in Python - GeeksforGeeks
July 23, 2025 - In this example, the file ('example.txt') is opened, and its content is read line by line in a loop using the readline() method. Each line is appended to the file_content string, creating a single string containing the entire file content.
🌐
AskPython
askpython.com › home › read file as string in python
Read File as String in Python - AskPython
October 11, 2023 - But first, we need to use the open() function to open the file. Always remember to add replace() function along with read() function to replace the new line characters with specified characters so that the data returned looks even and more readable.
🌐
FROMDEV
fromdev.com › home › 2025 › may
How to Read File Stream Data to String in Python: Complete 2025 Guide - FROMDEV
May 10, 2025 - The most straightforward approach for reading an entire file into a string is using Python’s built-in open() function with the context manager pattern. pythondef read_file_to_string(file_path, encoding='utf-8'): """ Read the entire content ...
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.4 documentation
To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument.
🌐
Reddit
reddit.com › r/learnpython › raw strings in python
r/learnpython on Reddit: Raw strings in python
May 2, 2022 -

So I know that if you put r in front of a string python will make it a raw string.

However, python throws an error when I end a string with a \ such as in a file directory.

Example in question is r'C:\PycharmProjects\pythonProject2\files\'. This ends up giving me an error of SyntaxError: EOL while scanning string literal.

This is fixed if I add a second ' to the end of the file directory so that it reads r'C:\PycharmProjects\pythonProject2\files\'', but now if I want to create a file in this directory its created as 'file_name.txt as opposed to file_name.txt.

I'm using pycharm as my compiler if that makes any difference.

🌐
Delft Stack
delftstack.com › home › howto › python › read file to string python
How to Read File to a String in Python | Delft Stack
March 11, 2025 - This tutorial demonstrates how to read a file to a string in Python. Explore various methods including read(), readlines(), and pathlib's read_text() to efficiently handle file content. Perfect for beginners and experienced programmers alike, this guide provides clear examples and explanations ...