Strings
A string (str) is Python’s built-in type for representing text: any sequence of characters, from a single letter to an entire document. Strings are one of the most frequently used types in Python, and understanding how they behave — especially their immutability — is essential.
Creating Strings
You can create a string with single quotes, double quotes, or triple quotes. Single and double quotes behave identically — pick whichever lets you avoid escaping an apostrophe or quotation mark inside the text.
single = 'Hello, world!' double = "Hello, world!" mixed = "It's a sunny day" # double quotes avoid escaping the apostrophe also_mixed = 'She said "hi"' # single quotes avoid escaping the double quotes print(single == double) # True - just different syntax for the same string type
Triple-Quoted and Multi-line Strings
Triple quotes (""" or ''') let a string span multiple lines without needing explicit newline characters, and are also used for documentation strings (docstrings).
poem = """Roses are red, Violets are blue, Python is fun, And so are you.""" print(poem) # Roses are red, # Violets are blue, # Python is fun, # And so are you.
Strings Are Immutable
Once a string is created, it can never be changed in place. Every operation that appears to “modify” a string — concatenation, replacement, uppercasing — actually builds and returns a brand-new string object, leaving the original untouched.
text = "hello" # text[0] = "H" # TypeError: 'str' object does not support item assignment new_text = "H" + text[1:] # instead, build a new string print(new_text) # Hello print(text) # hello -- the original is unchanged
Indexing and Slicing
Since a string is a sequence of characters, you can access individual characters by index (starting at 0) or extract a sub-string with a slice. Negative indices count from the end.
word = "Python" print(word[0]) # P -- first character print(word[-1]) # n -- last character print(word[0:3]) # Pyt -- characters at index 0, 1, 2 (3 is excluded) print(word[3:]) # hon -- from index 3 to the end print(word[:2]) # Py -- from the start up to (not including) index 2 print(word[::-1]) # nohtyP -- reversed, using a step of -1 print(word[::2]) # Pto -- every second character
Concatenation and Repetition
The + operator joins strings together, and * repeats a string a given number of times. Both produce a new string.
greeting = "Hello" + ", " + "world" + "!" print(greeting) # Hello, world! separator = "-" * 20 print(separator) # -------------------- border = "=" * 3 + " Title " + "=" * 3 print(border) # === Title ===
Escape Sequences
Escape sequences let you embed special characters — newlines, tabs, quotes, backslashes — inside a normal string literal.
Escape | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\ | Literal backslash |
' | Literal single quote (inside a '...' string) |
" | Literal double quote (inside a "..." string) |
\r | Carriage return |
print("Line one\nLine two") # newline
print("Name:\tAda") # tab
print("C:\\Users\\ada") # literal backslashes
print('It\'s here') # escaped single quoteRaw Strings
Prefixing a string literal with r creates a raw string, where backslashes are treated as literal characters instead of the start of an escape sequence. This is especially useful for regular expressions and Windows-style file paths.
path = r"C:\Users\ada\notes.txt" print(path) # C:\Users\ada\notes.txt -- backslashes stay literal pattern = r"\d+\.\d+" # a regex meaning "one or more digits, a dot, one or more digits" print(pattern)
String Length
The built-in len() function returns the number of characters in a string.
print(len("Python")) # 6
print(len("")) # 0
print(len(" spaced ")) # 10 -- whitespace counts tooStrings and Their Elements
Strings can be created with single, double, or triple quotes.
Strings are immutable — no in-place modification is possible.
Indexing and slicing let you read (but never write) characters or sub-strings.
+concatenates and*repeats, both producing new strings.Raw strings (
r"...") disable escape-sequence processing.len()returns the character count of any string.