Python - String String Literals

Introduction

Method Value
Single quotes 'spa"m'
Double quotes "spa'm"
Triple quotes '''... test ...''', """... test ..."""
Escape sequences "s\t \na\0m"
Raw stringsr"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:

Demo

print( 'test', "test" )

Result

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:

Demo

print( 'knight"s', "knight's" )

Result

Python automatically concatenates adjacent string literals in any expression.

Demo

title = "Meaning " 'of' " Life"        # Implicit concatenation 
print( title )

Result

Adding commas between these strings would result in a tuple, not a string.

You can also embed quote characters by escaping them with backslashes:

Demo

print( 'knight\'s', "knight\"s" )

Result