Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français
Lire en français

Python string methods: split, join, strip, replace

9 min read
Improve this page

Logo Python

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 split and rebuild it with join.
  • 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' → unchanged
print(uppercase) # 'PYTHON' → new string

In 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)
  • in tests mere presence.
  • startswith/endswith check the start or the end (perfect for filtering file extensions).
  • find returns the position (or -1 if absent); index does 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.
  • split cuts a string into a list, join assembles a list into a string (called on the separator).
  • strip/lstrip/rstrip clean whitespace at the edges (or, with an argument, a set of characters).
  • replace replaces all occurrences; a third argument limits the count.
  • upper/lower normalize the case, often before a comparison.
  • in, startswith/endswith, find search inside a string.
  • Trap: strip("word") removes characters, not the word; use removeprefix/removesuffix (3.9).
  • isdigit and isalpha test the nature of the characters; zfill, ljust and rjust align a string.
  • For formatting, see f-strings; for patterns, regular expressions.

FAQ: string methods in Python