Python Glossary Русский

all Python 3Strings (str)term

Creating strings

creation

Strings are written with single or double quotes, triple quotes for multi-line text, raw strings (r''), bytes (b'') and escape sequences.

Syntax

s = 'text' | s = "text" | s = '''multi''' | s = r'raw' | s = b'bytes'

Adjacent string literals are joined at compile time: 'abc' 'def' is the single string 'abcdef'. That is why a missing comma in ['a', 'b' 'c'] raises nothing and silently yields two items instead of three. Also, b'...' is not a string but a bytes object: mixing bytes with str gives a TypeError or a silent False, so convert explicitly with decode()/encode().

Examples

s1 = 'hello'
# → 'hello'
s2 = "world"
# → 'world'
s3 = '''line1
line2
line3'''
# → 'line1\nline2\nline3'
s4 = r'C:\Users\name'
# → 'C:\\Users\\name' (без escape)
s5 = b'bytes'
# → b'bytes'
s6 = 'tab:\there'
# → 'tab:\there' (\t — табуляция)
s7 = '\u041f\u0440\u0438\u0432\u0435\u0442'
# → 'Привет'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy