Files
freeCodeCamp/guide/english/python/escape-sequences/index.md
Corey Abma 723fa03609 update: added additional note fields (#29551)
* fix: corrected placement of notes

Some "note" fields were preemptive in their placement; I corrected their placement to be in more appropriate locations.

* update: added additional note fields

Added some additional "note" fields to assist those who are reading through the PHP documentation.

* fix: formatting of escape sequence examples

The formatting was a bit jumbled, so I cleaned up the formatting of the escape sequence.

* update: improved examples

I added a new example to demonstrate the use of a "break" statement in a while loop, as well as numbered each example.

* update: add clarifying statement

I added a note at the end of the description, with the goal of being a bit more explicit in terms of what the "continue" statement does.
2018-12-13 00:51:39 -08:00

1.8 KiB

title
title
Python Escape Sequences

A list of escape sequences can be found here

Escape sequences allow for including special characters into strings.

>>> print('Single quote strings can have \'single\' quotes if they are escaped')
"Single quote strings can have 'single' quotes if they are escaped"
>>> print("Double quote strings can have \"double\" quotes if they are escaped")
'Double quote strings can have "double" quotes if they are escaped'
>>> print("Multiline strings\ncan be created\nusing escape sequences.")
Multiline strings
can be created
using escape sequences.
>>> print("Backslashes \\ need to be escaped.")
Backslashes \ need to be escaped.

A raw string can be used by prefixing the string with r or R which allows for backslashes to be included without the need to escape them -

>>> print(r"Backslashes \ don't need to be escaped in raw strings.")
Backslashes \ don't need to be escaped in raw strings.
>>> print(r"An odd number of backslashes at the end of a raw string will cause an error\")
  File "<stdin>", line 1
    print(r"An odd number of backslashes at the end of a raw string will cause an error\")
                                                                                         ^
SyntaxError: EOL while scanning string literal.

Some more examples of escape sequences.

Escape Sequence <- Intended Character

  • \\ <- backslash
  • \' <- single quote / apostrophe
  • \" <- double quote / quotation mark
  • \a <- ASCII bell makes ringing the bell alert sounds ( eg. xterm )
  • \b <- ASCII backspace ( BS ) removes previous character
  • \n <- newline
  • \r <- carriage return