Python: Quote String

, , …,

Strings are enclosed using single quote or double quote. example

a = "this "
b = 'and that'
print a, b

You can use \n for linebreak, and \t for tab, etc.

a = "this\nthat\n" # use \n for line-break
b = 'more\nthings' # single quote works too
print a, b

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

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

You can 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

Python string quote summary:

blog comments powered by Disqus