Post
Strings in Python: slicing, methods and manipulation
Python strings: quotes and escapes, raw and triple-quoted strings, indexing and slicing, immutability, the common str methods for changing, splitting and searching text.
A string (str) is text. Python has a lot of tools for working with it, and most of them come built in as methods on the string itself.
Every code example here was run with Python 3.13, and the behavior was checked against the official tutorial and the string methods reference.
Writing strings
Single and double quotes give the same result. To put a quote inside a string, escape it with a backslash or use the other kind of quote:
print('He said "hi"')
print("It's fine")
print("Tab\there")
He said "hi"
It's fine
Tab here
A raw string, with an r before the quote, keeps backslashes as they are. It’s useful for Windows paths and regular expressions:
print(r"C:\new\folder")
C:\new\folder
Triple quotes (""" or ''') allow line breaks:
print("""multi
line""")
multi
line
Two string literals written next to each other are joined automatically. That works only for literals, not for variables.
Indexing and slicing
Each character has an index. The first is 0, and negative indexes count from the end:
s = "Hello, World"
print(s[0], s[-1])
H d
A slice s[start:end] takes characters from start up to, but not including, end:
print(s[0:5])
print(s[7:])
print(s[::-1])
Hello
World
dlroW ,olleH
s[::-1] takes every character with a step of -1, which reverses the string.
len() returns the number of characters:
print(len(s))
12
Strings are immutable
You can read a character, but you can’t change one in place:
t = "immutable"
t[0] = "I"
TypeError: 'str' object does not support item assignment
Methods don’t change the original string either. They return a new one, so you have to keep the result (s = s.upper()):
s = "Hello, World"
s.upper()
print(s)
print(s.upper())
Hello, World
HELLO, WORLD
Changing text
print(s.lower(), s.upper())
print(s.replace("World", "Python"))
print("hello world".title(), "hello".capitalize())
print(" padded ".strip() + "|")
print("ab" * 3)
hello, world HELLO, WORLD
Hello, Python
Hello World Hello
padded|
ababab
replace(old, new)replaces every occurrence.strip()removes spaces from both ends.lstrip()andrstrip()work on one side.*repeats a string.
Splitting and joining
split() turns a string into a list, and join() does the reverse, using the string it’s called on as the separator:
print("a,b,c".split(","))
print("-".join(["2026", "10", "11"]))
['a', 'b', 'c']
2026-10-11
Searching
find() returns the index of the first match, or -1 if there isn’t one. index() returns the same index, but raises an error:
print(s.find("World"), s.find("xyz"))
print(s.count("l"))
print(s.startswith("Hell"), s.endswith("!"))
7 -1
3
True False
s.index("xyz")
ValueError: substring not found
Use find() when a missing value is normal, and index() when it should be an error.
Padding and checking content
print("42".zfill(5), "7".rjust(3, "0"))
print("abc".isalpha(), "123".isdigit())
00042 007
True True
zfill() and rjust() pad to a width. Pass a fill character as the second argument of rjust() if you don’t want spaces.
What’s next
Next: f-strings, which put values and number formats inside a string.