Python Glossary Русский

all Python 3Data typesterm

str

strings

An immutable sequence of Unicode characters. Provides a rich set of formatting, searching and conversion methods.

Syntax

str([object]) | "текст" | 'текст'

Strings are immutable, so methods like upper(), replace() and strip() don't change the string in place but return a new one — you must assign the result or it's lost. For the same reason, building a string with += in a loop is quadratic; collect the pieces in a list and join them once with "".join().

Examples

greeting = "Привет"  # строковый литерал
full = "Иван" + " " + "Иванов"  # конкатенация → Иван Иванов
line = "-" * 20  # умножение → --------------------
print(len("Python"))  # → 6
print("Py" in "Python")  # → True
print("Python"[0])  # → P (индексирование)
print("Python"[1:4])  # → yth (срез)
print(str(42), str(3.14), str(None))  # → 42 3.14 None (приведение к строке)
print(repr(str(42)))  # → '42' (результат — строка, а не число)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy