Python strings come with dozens of methods to handle text: split, assemble, clean, replace, change case. These are very frequent operations in a script, from log processing to reading CSV files. This guide covers the essential methods: split and join (cut and assemble), strip (clean), replace (substitute), the case methods (upper, lower), searching inside a string, content tests (isdigit), alignment (zfill) and the modern removeprefix/removesuffix.
It is aimed at beginners who handle text and at anyone who automates data processing. One point shapes everything else: in Python, strings are immutable, so these methods always return a new string. For formatting (in French) with f-strings and for slicing (in French), see the dedicated guides. Every example was run with Python 3.12.
What you will learn
- Split a string with
splitand rebuild it withjoin. - Clean whitespace with
strip,lstrip,rstrip. - Replace text with
replace. - Change the case and search inside a string.
- Test the content of a string (
isdigit,isalpha) and align it (zfill,ljust,rjust). - Cleanly remove a prefix or suffix with
removeprefix/removesuffix.
Why are strings immutable?
First, a rule that explains the behavior of every method: a Python string cannot be modified after it is created. Each method therefore returns a new string and leaves the original untouched.
word = "python"uppercase = word.upper()
print(word) # 'python' → unchangedprint(uppercase) # 'PYTHON' → new stringIn practice, to keep the result you must reassign it (word = word.upper()) or store it in a variable. A method called without capturing its return value does nothing useful.
How do you change the case of a string?
The case methods convert between uppercase and lowercase. They are often used to normalize a text before comparing it.
"python".upper() # 'PYTHON'"PYTHON".lower() # 'python'"hello".capitalize() # 'Hello' (first letter)"john smith".title() # 'John Smith' (each word)A very common use case is the case-insensitive comparison: put both strings in lowercase before comparing them.
answer = "YES"if answer.lower() == "yes": print("Confirmed")How do you clean a string with strip?
strip() removes whitespace (and newlines) at the start and end of a string. It is essential to clean up user input or a line read from a file.
" Hello World ".strip() # 'Hello World'" text".lstrip() # 'text' (left only)"text ".rstrip() # 'text' (right only)These methods also accept a set of characters to remove, which leads to a frequent trap detailed below.
How do you search and test a string?
To know whether a text contains, starts with or ends with a pattern, several tools exist. The simplest is the in operator.
"Py" in "Python" # True"report.pdf".endswith(".pdf") # True"http://x".startswith("http") # True"abcabc".find("c") # 2 (position, or -1 if absent)"abcabc".count("a") # 2 (number of occurrences)intests mere presence.startswith/endswithcheck the start or the end (perfect for filtering file extensions).findreturns the position (or-1if absent);indexdoes the same but raises an error if absent.
How do you replace text with replace?
replace() replaces all occurrences of a text with another.
"cat cat".replace("cat", "dog") # 'dog dog'"a a a".replace("a", "b", 2) # 'b b a' (limited to 2)A third argument limits the number of replacements. To replace by pattern rather than exact text, move on to regular expressions (in French) with re.sub.
How do you split and assemble a string with split and join?
This is a duo widely used to process data. split() turns a string into a list, and join() does the opposite.
# split: string -> list"2026-07-02".split("-") # ['2026', '07', '02']"Python is great".split() # ['Python', 'is', 'great'] (on whitespace)
# join: list -> string (called on the separator)"-".join(["2026", "07", "02"]) # '2026-07-02'With no argument, split() cuts on whitespace and ignores repeated spaces, which is ideal for splitting a sentence into words. To split a text into lines, splitlines() handles every kind of line break:
"line1\nline2\nline3".splitlines() # ['line1', 'line2', 'line3']How do you test the content of a string?
Several methods return a boolean depending on the nature of the characters, which is useful to validate an input.
"42".isdigit() # True (digits only)"abc".isalpha() # True (letters only)"abc123".isalnum() # True (letters or digits)" ".isspace() # True (whitespace only)How do you remove a prefix or a suffix?
A frequent trap awaits anyone who wants to remove a word at the edge of a string. You think strip("pattern") removes the word pattern. In reality, strip removes characters, not a substring: from each edge, it removes every character that belongs to the given set. Here is what rstrip gives, which works from the end:
"test.txt".rstrip(".txt") # 'tes' → surprise!The given set is {'.', 't', 'x'}. Starting from the end, rstrip removes t, x, t, then ., then the last t of test, because it also belongs to the set. It stops at the s, the first character absent from the set. rstrip therefore never looks for the whole .txt suffix.
To remove an exact prefix or suffix, Python 3.9 introduced removeprefix and removesuffix, which process the whole string:
"img_photo.png".removeprefix("img_") # 'photo.png'"report.pdf".removesuffix(".pdf") # 'report'"test.txt".removesuffix(".txt") # 'test'Remember the distinction: strip cleans whitespace or a set of characters at the edges, whereas removeprefix and removesuffix remove an exact text at the start or at the end.
How do you align and pad a string?
To produce aligned output (tables, identifiers), a few methods pad a string to a given width.
"42".zfill(5) # '00042' (zeros on the left)"left".ljust(10) # 'left '"right".rjust(10) # ' right'Key takeaways
- Strings are immutable: each method returns a new string, to be reassigned.
splitcuts a string into a list,joinassembles a list into a string (called on the separator).strip/lstrip/rstripclean whitespace at the edges (or, with an argument, a set of characters).replacereplaces all occurrences; a third argument limits the count.upper/lowernormalize the case, often before a comparison.in,startswith/endswith,findsearch inside a string.- Trap:
strip("word")removes characters, not the word; useremoveprefix/removesuffix(3.9). isdigitandisalphatest the nature of the characters;zfill,ljustandrjustalign a string.- For formatting, see f-strings; for patterns, regular expressions.
FAQ: string methods in Python
split() method cuts a string and returns a list:"a,b,c".split(",") # ['a', 'b', 'c']
"Python is great".split() # ['Python', 'is', 'great']
- With a separator, it cuts on that separator.
- With no argument, it cuts on whitespace (and ignores repeated spaces).
splitlines():"l1\nl2\nl3".splitlines() # ['l1', 'l2', 'l3']
join() method is the reverse of split: it assembles a list of strings with a separator. You call it on the separator:"-".join(["2026", "07", "02"]) # '2026-07-02'
", ".join(["apple", "pear"]) # 'apple, pear'
Warning: every element must be a string. To join numbers, convert them first:", ".join(str(n) for n in [1, 2, 3]) # '1, 2, 3'
strip() removes whitespace at the start and end of a string:" Hello ".strip() # 'Hello'
" Hello ".lstrip() # 'Hello ' (left only)
" Hello ".rstrip() # ' Hello' (right only)
They also accept a set of characters to remove: "...text...".strip(".").Since strings are immutable, these methods return a new string and leave the original untouched.replace() method replaces all occurrences:"cat cat".replace("cat", "dog") # 'dog dog'
A third argument limits the number of replacements:"a a a".replace("a", "b", 2) # 'b b a'
To replace by pattern (rather than exact text), move on to regular expressions (in French) with re.sub."python".upper() # 'PYTHON'
"PYTHON".lower() # 'python'
"hello".capitalize() # 'Hello'
"john smith".title() # 'John Smith'
Common use: normalize before comparing, to ignore the case:if user_input.lower() == "yes":
...
strip removes characters, not a substring:"template.py".strip("template") # '.py' (eats t, e, m, p, l, a...)
To remove an exact prefix or suffix, use removeprefix and removesuffix, added in Python 3.9:"img_photo.png".removeprefix("img_") # 'photo.png'
"report.pdf".removesuffix(".pdf") # 'report'
These methods process the whole string, not character by character.