Python 3: Quoting String
Strings are enclosed using single quote or double quote. example
# -*- coding: utf-8 -*- # python 3 a = "this " b = 'and that' print(a, b)
You can use
\n for linebreak, and
\t for tab, etc.
# -*- coding: utf-8 -*- # python 3 a = "this\nthat\n" # use \n for line-break b = 'more\nthings' # single quote works too print(a, b) # output: # this # that # more # things
To quote a string of multiple lines, use triple quotes. Example:
# -*- coding: utf-8 -*- # python 3 d = """this will be printed in 3 lines""" print(d) # output: # this # will be printed # in 3 lines
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”)
# -*- coding: utf-8 -*- # python 3 c = r"this \n and that" print(c) # prints a single line
- 'Single' quote and "double" quote chars are equivalent. (linebreak in your string is not allowed. It's syntax error.)
- The backslash char is a char escape mechanism.
rin front of your quoting char (For example,
r"hot") if you want backslash to be literal.
- If your string is multiple line, use triple
'''single'''quote or triple
"""double"""quote. You can still prefix
If you have a question, put $5 at patreon and message me.