Unicode Characters in an Ordered Sequence
In Python, a string is an immutable sequence of Unicode characters. Its order matters: every character has a position, and operations such as indexing and slicing use that order.
Strings have three main characteristics you need to understand:
- Ordered sequence: Each character has a fixed position.
- Written as a literal: Single or double quotes delimit the text in source code.
- Type
str: Python stores the resulting value as a string.
Python counts positions by Unicode code point, which need not match one visible symbol. For example, len("é") is 1, but len("e\u0301") is 2: the second form contains e and a separate combining accent.
# Example string with single quotes
>>> 'hello'
'hello'
# Example string with double quotes
>>> "hello"
'hello'
# Displaying string with print
>>> print("hello")
hello
# Storing string in a variable
>>> text = 'hello'
>>> text
'hello'The interactive prompt shows the string's representation, so Python may choose single or double quotes to keep that representation readable. print() writes the string value itself, without the literal delimiters.
Empty Strings and the Str Function
Python supports empty strings and conversion from other data types to strings. An empty string contains no characters and has length zero, while str() builds a text representation of any value.
Empty Strings
An empty string contains no characters and has length zero. You can write one with a pair of quotes containing nothing.
# Creating an empty string
>>> empty_text = ""
>>> empty_text
''The Str Function
The str() function returns a readable string representation of a value. Use it when text must include a number or another non-string value.
# Converting number to string
>>> str(5)
'5'
# Converting float to string
>>> xstr = str(3.4)
>>> print(xstr)
3.4Multiline Strings
Python distinguishes between splitting source code across lines and storing actual newline characters inside a string. A line can wrap in the source without adding a newline to the value, while a triple-quoted string keeps every line break.
Line Continuation Character
A backslash at the end of a physical source line explicitly continues the statement. In this example, the continued statement contains two adjacent literals, which Python concatenates without adding a newline to the value.
# Continue the source statement with a backslash
>>> text = 'xxx' \
... 'yyy'
>>> text
'xxxyyy'
>>> print(text)
xxxyyyLine Continuation with Parentheses
Inside parentheses, Python continues a statement across line breaks without a backslash. Comments or spaces at the end of a line do not break this continuation, and moving a line does not require moving a continuation character with it.
# Continue the statement inside parentheses
>>> text = (
... 'xxx'
... 'yyy'
... )
>>> text
'xxxyyy'
>>> print(text)
xxxyyyTriple Quotes
Triple-quoted literals can store actual line breaks. Those line breaks become \n characters in the string value.
# Triple-quoted string with real line breaks
>>> text = '''xxx
... yyy
... zzz'''
>>> text
'xxx\nyyy\nzzz'
>>> print(text)
xxx
yyy
zzzTriple double quotes behave the same way. Choose the delimiter that makes the text easiest to read and escape.
String Literal Concatenation
Python can combine string literals while reading the source or combine string values while the program runs. Adjacent literals join with no operator between them, while values held in variables need an operator.
Adjacent Literal Concatenation
Python joins adjacent string literals while it reads the source code. This shortcut works only for text written directly as literals.
# Adjacent string literals are automatically combined
>>> 'hello ' 'world'
'hello world'
# Combining multiple strings at once
>>> "hel" 'lo' " world" '!'
'hello world!'Plus Operator
The + operator concatenates two string values at runtime.
# Using + operator for concatenation
>>> 'hello ' + 'world'
'hello world'Multiplication Operator
The * operator repeats a string an integer number of times.
# Repeating string with * operator
>>> 3 * '+o+'
'+o++o++o+'String Variable Concatenation
Variables hold values. Use an operator such as + when you combine those values. Adjacent literals are joined while the source is read, which is why "a" "b" works but name other does not.
Using Operators
Use + to concatenate string values and * with an integer to repeat one. Simply placing variable names next to each other is invalid syntax.
# Defining string variables
>>> text1 = 'hello'
>>> text2 = 'world!'
>>> text3 = '+o+'
>>> a = 3
# Combining with + operator
>>> text1 + ' world!'
'hello world!'
>>> 'hello ' + text2
'hello world!'
>>> text1 + ' ' + text2
'hello world!'
# Repeating with * operator
>>> a * text3
'+o++o++o+'
>>> text3 * a
'+o++o++o+'Common Errors
The following forms fail while Python parses the code because there is no operator between a variable and the next expression.
# ERROR: Cannot combine variables without operators
>>> text1 'world'
SyntaxError ...
>>> 'hello ' text2
SyntaxError ...
>>> text1 text2
SyntaxError ...