In Python, you can use escape character sequences to include special characters in strings. An escape character is a backslash () followed by a character that represents a special meaning, such as a newline or a quote.
Here is a list of some common escape character sequences in Python:
\n
: Newline\r
: Carriage return\t
: Tab\\
: Backslash\'
: Single quote\"
: Double quote
Here are some examples of using escape character sequences in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Copy code<code># Print a newline print("Hello,\nWorld!") # Print a tab print("Hello,\tWorld!") # Print a backslash print("Hello, \\World!") # Print a single quote print('Hello, \'World!\'') # Print a double quote print("Hello, \"World!\"") |
It’s also important to note that you can use triple quotes (single or double) to define a string that spans multiple lines, without having to use escape characters for newlines.
Here is an example of using triple quotes to define a multiline string:
1 2 3 |
Copy code<code>multiline = """This is a multiline string.""" |