0% found this document useful (0 votes)
1 views

Python Escape Codes (1)

The document explains Python escape characters, which are used to insert illegal characters in strings by using a backslash followed by the character. It provides examples of common escape characters such as single quotes, backslashes, new lines, and tabs. Additionally, it discusses the use of comments in Python to make code more readable.

Uploaded by

starp1066
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python Escape Codes (1)

The document explains Python escape characters, which are used to insert illegal characters in strings by using a backslash followed by the character. It provides examples of common escape characters such as single quotes, backslashes, new lines, and tabs. Additionally, it discusses the use of comments in Python to make code more readable.

Uploaded by

starp1066
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

https://www.w3schools.

com/python/gloss_python_esc
ape_characters.asp

Python Escape Characters


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:

Code Result Try it

Page 1 of 6
\' 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


»

Page 2 of 6
Related Pages

Python Strings Tutorial String Literals Assigning a String to a Variable Multiline

Strings Strings are Arrays Slicing a String Negative Indexing on a String String

Length Check In String Format String

❮ Python Glossary

https://cscircles.cemc.uwaterloo.ca/3-comments-literals/

Comments and Quotes


A computer program looks like a code language, which is necessary for the computer to
precisely understand what your commands mean. But, being a code language makes it
harder for humans to read. To compensate for this, you are allowed to write extra notes in
your program that the computer ignores. These notes are called comments.
In Python, any line of instructions containing the # symbol ("pound sign" or "hash")
denotes the start of a comment. The rest of the line will be ignored when the program is
run. Here is an example.
Example
The effect of a comment.
print(1) # he
# print(2)
print(3) # an

Page 3 of 6
Run program

Because the second line started with a # sign, Python totally ignored that line, and as you
can see, the number 2 was not printed. Common uses for comments include:
▪ explaining parts of the program, for you or other people to read later;
▪ leaving "to do" notes, when you write a longer program;
▪ temporarily disabling ("commenting out") a line of a program without totally deleting
it, so that it is easier to put back in later.
Here is an exercise to illustrate. If you edit the code too much and want to bring back the
default version of the code, select Reset code to default.

Coding Exercise: Second Guessing


Debug this program so that it prints out the number of seconds in a week. Hint
# goal: print
secondsPerM
secondsPerH
secondsPerD
daysPerWee
# daysPerWe
print(second

Run program

Strings
Strings are sequences of letters and numbers, or in other words, chunks of text. They are
surrounded by two quotes for protection: for example in Lesson 0 the part "Hello, World!" of
the first program was a string. If a pound sign # appears in a string, then it does not get
treated as a comment:
Example
A string containing the # sign.
myStr = "To
print(myStr)

Run program

This behaviour is because the part inside the quotes "" is a string literal, meaning that it
should be literally copied and not interpreted as a command. Similarly, print("3 + 4") will not
print the number 7, but just the string 3 + 4.

Escape Sequences
Page 4 of 6
What if you want to include the quote character " inside of a string? If you try to execute
print("I said "Wow!" to him") this causes an error: the problem is that Python sees one string "I
said " followed by something Wow! which is not in the string. This is not what we intended!
Python does have two simple ways to put quote symbols in strings.
▪ You are allowed to start and end a string literal with single quotes (also known as
apostrophes), like 'blah blah'. Then, double quotes can go in between, such as 'I said
"Wow!" to him.'
▪ You can put a backslash character followed by a quote (\" or \'). This is called an
escape sequence and Python will remove the backslash, and put just the quote in the
string. Here is an example.

Example
The backslashes protect the quotes, but are not printed.
print('Here\'
print("This is

Run program

Furthermore, because of escape sequences, backslash (\) is a special character. So to


include a backslash in a string, you actually need to "escape it" with a second backslash, or
in other words you need to write \\ in the string literal.

Multiple Choice Exercise: Escape Characters


What is the output of print("Backslashes \\ and single quotes \' and double quotes \" and pound signs #
are awesome!")
Your choice:
and pound signs

Check answer

Coding Exercise: The Great Escape


Write a program that prints the following:
A double-quote's escaped using a backslash, e.g. \"

Page 5 of 6
Note: there is more than one way to do this (as usual)! For example, you could enclose the
whole string in single quotes, or in double quotes. In either case, escaping is needed. For
extra practice, solve the problem one way, and then solve it the other way. Can you solve it
a third way?

Run program

Page 6 of 6

You might also like