For AI agents: use /llms.txt for the Nakafa content index.
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:
str: Python stores the resulting value as a string.# 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.
Python allows you to create empty strings and convert other data types to 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 asks a value for its readable string form. 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.4Python distinguishes between splitting source code across lines and storing actual newline characters inside a string.
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)
xxxyyyInside parentheses, Python continues the statement implicitly. This is usually easier to read and safer to edit than an explicit backslash.
# Continue the statement inside parentheses
>>> text = (
... 'xxx'
... 'yyy'
... )
>>> text
'xxxyyy'
>>> print(text)
xxxyyyTriple-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.
Python can combine string literals while parsing the source or combine string values while the program runs.
Adjacent string literals are concatenated while Python parses the source. This syntax works only with literals, not variables.
# Adjacent string literals are automatically combined
>>> 'hello ' 'world'
'hello world'
# Combining multiple strings at once
>>> "hel" 'lo' " world" '!'
'hello world!'The + operator concatenates two string values at runtime.
# Using + operator for concatenation
>>> 'hello ' + 'world'
'hello world'The * operator repeats a string an integer number of times.
# Repeating string with * operator
>>> 3 * '+o+'
'+o++o++o+'Variables hold values rather than literal tokens, so they need an operator when you combine them.
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+'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 ...