How do you input a raw string in python?

1 Answer

Sorted by: Reset to default

1

I tried this path = input(r'Input your path:')

It seems that path is something like paht = \path\the\user\chose

By the way, I use Python3.

Improve this answer

answered Sep 6, 2019 at 1:10

How do you input a raw string in python?

time Offtime Off

211 bronze badge

1

  • Thanks bro, its really working for me, you saved my lot of time.

    – Ali

    Oct 30, 2021 at 6:29

Add a comment  | 

Your Answer

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name

Email

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged regexstringpython-2.7 or ask your own question.

In Python, strings prefixed with r or R, such as r'...' and r"...", are called raw strings and treat backslashes \ as literal characters. Raw strings are useful when handling strings that use a lot of backslashes, such as Windows paths and regular expression patterns.

This article describes the following contents.

  • Escape sequences
  • Raw strings treat backslashes as literal characters
  • Convert normal strings to raw strings with repr()
  • Raw strings cannot end with an odd number of backslashes

Escape sequences

In Python, characters that cannot be represented in a normal string (such as tabs, line feeds. etc.) are described using an escape sequence with a backslash \ (such as \t or \n), similar to the C language.

  • 2. Lexical analysis - String and Bytes literals — Python 3.9.7 documentation

s = 'a\tb\nA\tB'
print(s)
# a b
# A B

Raw strings treat backslashes as literal characters

Strings prefixed with r or R, such as r'...' and r"...", are called raw strings and treat backslashes \ as literal characters. In raw strings, escape sequences are not treated specially.

rs = r'a\tb\nA\tB'
print(rs)
# a\tb\nA\tB

There is no special type for raw strings; it is just a string, which is equivalent to a regular string with backslashes represented by \\.

print(type(rs))
# 

print(rs == 'a\\tb\\nA\\tB')
# True

In a normal string, an escape sequence is considered to be one character, but in a raw string, backslashes are also counted as characters.

  • Get the length of a string (number of characters) in Python

print(len(s))
# 7

print(list(s))
# ['a', '\t', 'b', '\n', 'A', '\t', 'B']

print(len(rs))
# 10

print(list(rs))
# ['a', '\\', 't', 'b', '\\', 'n', 'A', '\\', 't', 'B']

Windows paths

Using the raw string is useful when representing a Windows path as a string.

Windows paths are separated by backslashes \, so if you use a normal string, you have to escape each one like \\, but you can write it as is with a raw string.

path = 'C:\\Windows\\system32\\cmd.exe'
rpath = r'C:\Windows\system32\cmd.exe'
print(path == rpath)
# True

Note that a string ending with an odd number of backslashes raises an error, as described below. In this case, you need to write it in a normal string or write only the trailing backslash as a normal string and concatenate it.

path2 = 'C:\\Windows\\system32\\'
# rpath2 = r'C:\Windows\system32\'
# SyntaxError: EOL while scanning string literal
rpath2 = r'C:\Windows\system32' + '\\'
print(path2 == rpath2)
# True

Convert normal strings to raw strings with repr()

Use the built-in function repr() to convert normal strings into raw strings.

  • Built-in Functions - repr() — Python 3.9.7 documentation

s_r = repr(s)
print(s_r)
# 'a\tb\nA\tB'

The string returned by repr() has ' at the beginning and the end.

print(list(s_r))
# ["'", 'a', '\\', 't', 'b', '\\', 'n', 'A', '\\', 't', 'B', "'"]

Using slices, you can get the string equivalent to the raw string.

s_r2 = repr(s)[1:-1]
print(s_r2)
# a\tb\nA\tB

print(s_r2 == rs)
# True

print(r'\t' == repr('\t')[1:-1])
# True

Raw strings cannot end with an odd number of backslashes

Since backslashes escape the trailing ' or ", an error will occur if there are an odd number of backslashes \ at the end of the string.

  • Design and History FAQ - Why can’t raw strings (r-strings) end with a backslash? — Python 3.9.7 documentation

# print(r'\')
# SyntaxError: EOL while scanning string literal

print(r'\\')
# \\

# print(r'\\\')
# SyntaxError: EOL while scanning string literal

How do you write a string in Python?

To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable. You can look into how variables work in Python in the Python variables tutorial. For example, you can assign a character 'a' to a variable single_quote_character .

What is a raw string explain with example?

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.

What is raw string?

A raw string in programming allows all characters in a string literal to remain the same in code and in the material, rather than performing their standard programming functions. Raw strings are denoted with the letter r, or capital R, and might look something like this: R “(hello)”

What is raw string in Python regex?

According to Python docs, raw string notation (r"text") keeps regular expressions meaningful and confusion-free. Without it, every backslash ('\') in a regular expression would have to be prefixed with another one to escape it. For example, the two following lines of code are functionally identical − >>> re.