Python: Quote String

By Xah Lee. Date: . Last updated: .

What is String

A string is a sequence of characters.

Python string is immutable. Any function that work on string returns a new string.

Quoting String

Two ways to quote a string.

String Delimiter

String can be quoted by QUOTATION MARK e.g. "abc" or APOSTROPHE e.g. 'abc' .

There is no different between them.

No Here-String

Python does not suport here-doc or here-string, as in Perl, PHP, PowerShell.

Single Quote String

String can be created by:

they are effectively the same.

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

xx = "this is string"

yy = 'another string'

print(xx, yy)
# Literal linebreak is not allowed

xx = "abc
xyz"

#     xx = "abc
#             ^
# SyntaxError: EOL while scanning string literal
# error: cannot format -: Cannot parse: 1:3: a = "abc

Triple Quote String

String Prefix Char

a character may be prefixed to a string, to make the string of a particular type.

prefix chars:

these prefix characters may be combined. e.g. fr"abc" means can embed expression and also raw (ignore Python: String Escape Sequence)

String Escape

Inside string (except Raw String ):

Python, String