Method | Value |
---|---|
Single quotes | 'spa"m' |
Double quotes | "spa'm" |
Triple quotes | '''... test ...''', """... test ...""" |
Escape sequences | "s\t \na\0m" |
Raw strings | r"C:\new\test.spm" |
Bytes literals in 3.X and 2.6+ | b'sp\x01am' |
Unicode literals in 2.X and 3.3+ | u'eggs\u0020test' |
Single- and Double-Quoted Strings Are the Same.
Single- and double-quote characters are interchangeable.
The following two strings are identical:
print( 'test', "test" )
This allows you to embed a quote character of the other variety inside a string without escaping it with a backslash.
You may embed a single-quote character in a string enclosed in double-quote characters, and vice versa:
print( 'knight"s', "knight's" )
Python automatically concatenates adjacent string literals in any expression.
title = "Meaning " 'of' " Life" # Implicit concatenation print( title )
Adding commas between these strings would result in a tuple, not a string.
You can also embed quote characters by escaping them with backslashes:
print( 'knight\'s', "knight\"s" )