A Python string is an immutable sequence of Unicode characters, stored in the built-in str type. You create a Python string by putting text inside single quotes, double quotes or triple quotes, or by calling str() on another object. Strings are used for names, messages, file contents, URLs and almost every other piece of text in a program.
This guide is for beginners and uses Python 3.13 and 3.14. Every example can be pasted into a file or the interactive interpreter.
A string is an ordered collection of characters. "Ordered" means each character has a position, and "Unicode" means a string can hold letters from any language, digits, punctuation and emoji.
Here greeting is a name bound to a str object, type() shows the type and len() counts the characters (including the comma and the space).
There are four common ways to create a string.
Single and double quotes work the same way. Pick one style and use the other when the text itself contains a quote: "It's fine" or 'He said "hi"'. Triple quotes (''' or """) let the text continue across lines.
A backslash starts an escape sequence inside a normal string.
| Sequence | Meaning |
|---|---|
\n | New line |
\t | Tab |
\\ | A single backslash |
\' and \" | A quote character |
\u00e9 | The Unicode character with that code point (here e with an acute accent) |
Put an r before the opening quote to make a raw string, where backslashes are kept literally. Raw strings are handy for Windows paths and regular expressions.
Positions start at 0, and negative numbers count from the end.
Asking for an index that does not exist, such as word[10], raises IndexError. Slices never raise that error; they just return a shorter string.
A string is immutable: you cannot assign to one of its characters.
The last two lines build a new string and rebind the name. Every string method follows the same rule: it returns a new string and leaves the original alone.
Join and concatenate with care: "a" + "b" works, but "age: " + 30 raises TypeError because Python does not convert numbers automatically. Use an f-string instead.
F-strings (formatted string literals) are the standard way to insert values into text. Put f before the quote and write expressions inside curly braces.
The part after the colon is a format specifier: .1f means one decimal place, and , adds thousands separators. The {x = } form prints the expression and its value, which is useful for debugging. Since Python 3.12 you can reuse the same quote type inside an f-string expression and span multiple lines inside the braces.
It is str. Python 3 has no separate character type; a single character is a string of length 1. Raw binary data uses bytes, not str.
Use str(42) to get "42", then int("42") or float("3.5") to convert back. Invalid text such as int("abc") raises ValueError.
Use if not text:. An empty string is falsy, so this also works for the common case. Use text == "" when you want to be explicit.
Yes. Since Python 3, all str objects are Unicode, and source files are read as UTF-8 by default.
A Python string is an immutable str sequence of Unicode characters created with quotes, triple quotes or str(). You can index and slice it, call methods that return new strings, and build text with f-strings. Remember that strings never change in place, and that you must convert numbers explicitly when joining them with +.