How do you write special characters in a python file?

I'm using Python to process some plain text into LaTeX, so I need to be able to write things like \begin{enumerate} or \newcommand to a file. When Python writes this to a file, though, it interprets \b and \n as special characters.

How do I get Python to write \newcommand to a file, instead of writing ewcommand on a new line?

The code is something like this ...

with open(fileout,'w',encoding='utf-8') as fout:
    fout.write("\begin{enumerate}[1.]\n")

Python 3, Mac OS 10.5 PPC

jensgram

30.4k6 gold badges80 silver badges96 bronze badges

asked Nov 22, 2010 at 13:09

1

One solution is to escape the escape character (\). This will result in a literal backslash before the b character instead of escaping b:

with open(fileout,'w',encoding='utf-8') as fout:
    fout.write("\\begin{enumerate}[1.]\n")

This will be written to the file as

\begin{enumerate}[1.]<newline>

(I assume that the \n at the end is an intentional newline. If not, use double-escaping here as well: \\n.)

answered Nov 22, 2010 at 13:12

jensgramjensgram

30.4k6 gold badges80 silver badges96 bronze badges

1

You just need to double the backslash: \\n, \\b. This will escape the backslash. You can also put the r prefix in front of your string: r'\begin'. As detailed here, this will prevent substitutions.

answered Nov 22, 2010 at 13:12

asthasrasthasr

8,9441 gold badge28 silver badges40 bronze badges

0

You can also use raw strings:

with open(fileout,'w',encoding='utf-8') as fout:
    fout.write(r"\begin{enumerate}[1.]\n")

Note the 'r' before \begin

answered Nov 22, 2010 at 13:14

Marco MarianiMarco Mariani

13.4k6 gold badges38 silver badges55 bronze badges

2


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:

CodeResultTry it
\' 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 »



How do you add a special character to a string in Python?

Escape sequences allow you to include special characters in strings. To do this, simply add a backslash ( \ ) before the character you want to escape.

How do you print special characters in Python?

Use repr() to print special characters Call repr(string) to return string with special characters escaped.

How do you denote a character in Python?

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

What is the data type of special characters in Python?

A string is a collection of one or more characters put in a single quote, double-quote or triple quote. In python there is no character data type, a character is a string of length one.