Python: Quote String

By Xah Lee. Date: . Last updated: .

A string is a sequence of characters.

(in Python 3, it's a sequence of Unicode characters.)

(in Python 2, it's a sequence of bytes (representing ASCII Characters). But if prefixed with u, then it's a sequence of Unicode characters.)

Quote String by Double Quote

String can be created by double quote.

Literal linebreak in string is not allowed. It's syntax error.

a = "this"
print(a)

Quote String by Single Quote

String can also be created by single quote.

'Single quote' and "double quote" delimited strings are effectively the same.

b = 'that'
print(b)

String Escape, Backslash

Inside string:

see Python: String Escape Sequence

r for Raw string (No Backslash Escape)

Add r in front of the quote symbol. This way, backslash characters will be interpreted as is, not as escapes. (β€œr” for β€œraw”)

c = r"this \n and that"

print(c) # prints a single line

Triple Quote for Multi-Lines String

To quote a string of multiple lines, use triple quotes.

d = """this
will be printed
in 3 lines"""

print(d)

# output:

# this
# will be printed
# in 3 lines

Python 2, String Containing Unicode

Python String

Python

Overview

Help

Comment

String

Arithmetic

Boolean

Conditional

Loop

Data Structure

Function and Class

Module

Misc

Regex

Text Processing

Web

Misc