If you're reading paths out of a file, and passing them to functions like os.path.getsize
, you don't need to escape them. For example:
>>> with open['name with spaces', 'w'] as f:
... f.write['abc\n']
>>> os.path.getsize['name with spaces']
4
In fact, there are only a handful of functions in Python that need spaces escaped, either because they're passing a string to the shell [like os.system
] or because they're trying to do shell-like parsing on your behalf [like subprocess.foo
with an arg string instead of an arg list].
So, let's say logfile.txt
looks like this:
/Volumes/My Drive/My Scans/Batch 1/foo bar.tif
/Volumes/My Drive/My Scans/Batch 1/spam eggs.tif
/Volumes/My Drive/My Scans/Batch 2/another long name.tif
… then something like this will work fine:
with open['logfile.txt'] as logf:
for line in logf:
with open[line.rstrip[]] as f:
do_something_with_tiff_file[f]
Noticing those *
characters in your example, if these are glob patterns, that's fine too:
with open['logfile.txt'] as logf:
for line in logf:
for path in glob.glob[line.rstrip[]]:
with open[path] as f:
do_something_with_tiff_file[f]
If your problem is the exact opposite of what you described, and the file is full of strings that are escaped, and you want to unescape them, decode['string_escape']
will undo Python-style escaping, and there are different functions to undo different kinds of escaping, but without knowing what kind of escaping you want to undo it's
hard to say which function you want…
Escape Characters
To insert characters that are illegal in a string, use an escape character.
An escape character is a backslash \
followed by the character you want to insert.
An example of an illegal character is a double quote inside a string that is surrounded by double quotes:
Example
You will get an error if you use double quotes inside a string that is surrounded by double quotes:
txt = "We are the so-called "Vikings" from the north."
Try it Yourself »
To fix this problem, use the escape character \"
:
Example
The escape character allows you to use double quotes when you normally would not be allowed:
txt = "We are the so-called \"Vikings\" from the north."
Try it Yourself »
Other escape characters used in Python:
\' | Single Quote | Try it » |
\\ | Backslash | Try it » |
\n | New Line | Try it » |
\r | Carriage Return | Try it » |
\t | Tab | Try it » |
\b | Backspace | Try it » |
\f | Form Feed | |
\ooo | Octal value | Try it » |
\xhh | Hex value | Try it » |
In this python tutorial, we will discuss Escape sequence in Python. We will also check:
- What is the escape sequence?
- How to escape a single quote in python
- Python escape sequence n
- Python escape sequence backslash
- Python escape sequence for space
- Python escape sequence for backspace
- Python escape sequence for Hexa value
- Python escape sequence for an octal value
- Remove all escape sequence from a list in a string
- Escape character for space python
- Python escape sequence ignore
- Python escape sequence remove
What is the escape sequence?
- An escape sequence is a special character used in the form of backslash[\] followed by a character that is required.
- These characters are used to represent whitespace.
- Whitespace gives characters like space, tab, formfeed, vertical tab.
Escape sequence | Meaning |
\’ | This represents a single quote |
\n | This represents a newline |
\r | This represents a carriage return |
\t | This represents a tab |
\b | This represents a backspace |
\f | This represents a formfeed |
\ooo | This represents an octal value |
\xhh | This represents a hex value |
\\ | This represents a backslash |
\uxxxx | This represents 16 bits hex value |
\uxxxxxxxx | This represents 32 bits hex value |
How to escape single quotes in Python
Let us check an example of escape single quotes in Python, we can see how to use \’ single quote in strings in Python.
Example:
string = 'That\'s my bag.'
print[string]
Below screenshot shows that single quote is used for a word That’s.
Let us see an example of Python escape sequence n. I have used a “\n” newline character. A newline character is used to write the words in a new separate line.
Example:
string = "python\n guides"
print[string]
You can refer the below screenshot for the output, you can see the words in new separate line.
Python escape sequence backslash
Let us check an example of a Python escape sequence backslash. The backslash is an escape sequence, \\ is used to print a single backslash.
Example:
string = "python\\ guides"
print[string]
You can refer the below screenshot for the output:
Python escape sequence for space
In this example, I have used “\t” character to get space between the words.
Example:
string = "python\tguides"
print[string]
You can refer to the below screenshot for the output, we can see the space between the word python and guides.
Python escape sequence backspace
In this example, I have used “\b” to remove the space between the words in Python.
Example:
string = "python \bguides"
print[string]
You can see the output in the below screenshot.
Python escape sequence for Hexa value
Let us check an example of Python escape sequence for Hexa value, I have used \xhh to convert hexa value into a string.
Example:
string = "\x50\x59\x54\x48\x4f\x4E \x47\x55\x49\x44\x45\x53"
print[string]
In this belowscreenshot, we can see the converted string.
Python escape sequence for Octal value
Let us check an example of Python escape sequence for Octal value, I have used \ooo to convert the octal value into a normal string.
Example:
string = "\120\131\124\110\117\116 \107\125\111\104\105\123"
print[string]
You can refer the below screenshot for the output:
Remove all escape sequence from a list
In this example, I have used ‘\x50’ to remove all escape sequences by converting hex values into strings and ‘\x20’ represents the space.
Example:
x = ['welcome','\x50', 'to' '\x20','python','guides']
print[x]
In the below screenshot, we can see the output that the Hexa value ‘\x50’ is converted into ‘p’ and ‘\x20’ is converted into space.
Escape character for space python
In this example, I have used \t between the words to get space.
Example:
string = "python\tguides"
print[string]
In this output, we can see the space between the words.
Python escape sequence ignore
To ignore the escape sequence in a string we have to make the string as a raw string by placing r before the string.
Example:
string = r'python guides'
print[string]
You can refer the below screenshot for the output. In this output, we can see that the raw statement is ignored.
Python escape sequence remove
In this example, I have used string.split[] to remove character from left and right of the argument.
string = '\r\r\b pythonguides \r\r\n\b '
string.strip
print[string]
You can refer the below screenshot for the output:
You may like the following Python tutorials:
- Python list comprehension lambda
- Python Threading and Multithreading
- How to convert Python degrees to radians
- Python Comparison Operators
- Python namespace tutorial
- Python Tkinter Frame
- How to make a matrix in Python
- Linked Lists in Python
- Python ask for user input
In this Python tutorial, we have learned about the Escape sequence in python. Also, We covered these below topics:
- What is the escape sequence?
- How to escape a single quote in python
- Python escape sequence n
- Python escape sequence backslash
- Python escape sequence for space
- Python escape sequence for backspace
- Python escape sequence for Hexa value
- Python escape sequence for an octal value
- Remove all escape sequence from a list in a string
- Escape character for space python
- Python escape sequence ignore
- Python escape sequence remove
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.