Swapping one piece of text for another is one of the first real jobs you will give Python, whether you are cleaning a spreadsheet export or fixing a typo across a file. Python replace string work usually starts and ends with one method, str.replace(). This guide shows how it works, the mistake nearly everyone makes the first time, how to limit how many changes it makes, and when a different tool is the better fit. Facts come from the official Python 3.15.2 documentation, checked December 2, 2026.
VetBoss is not affiliated with, endorsed by or sponsored by the Python Software Foundation.
What str.replace() does
The official docs say replace() will "Return a copy of the string with all occurrences of substring old replaced by new." (Python docs: str.replace)
The basic form is text.replace(old, new). Here is a simple example:
note = 'Formation at 0800. Chow at 0800.'
fixed = note.replace('0800', '0900')
print(fixed)
That prints: Formation at 0900. Chow at 0900. Both times changed, because by default replace() changes every match.
The first-time mistake: not saving the result
Notice the docs say replace() returns a copy. It does not change the original string. That is because, in the docs' words, "Strings are immutable sequences of Unicode code points." Immutable means a string cannot be changed after it is created.
So this does nothing useful:
note = 'Formation at 0800.'
note.replace('0800', '0900')
print(note)
The print still shows 0800. The new string was created and then thrown away. Save it, either to a new name or back to the same one:
note = note.replace('0800', '0900')
If your replace seems to be doing nothing, this is the first thing to check.
Limiting the number of changes with count
replace() takes an optional third value. The docs explain: "If count is given, only the first count occurrences are replaced." Leave it out, and every match changes.
line = 'spam, spam, spam'
print(line.replace('spam', 'eggs', 1))
That prints: eggs, spam, spam. The docs use this exact example. They also note "Changed in version 3.13: count is now supported as a keyword argument." So on Python 3.13 or newer, you can write the clearer version:
line.replace('spam', 'eggs', count=1)
It is case-sensitive
replace() matches text exactly, including capital letters:
unit = 'Alpha Company'
print(unit.replace('alpha', 'Bravo'))
Nothing changes, because alpha and Alpha are different text. If you need to ignore case, the usual answer is a regular expression, covered below.
Planning your next career step? Check your eligibility.
Making several changes at once
You can chain calls, since each replace() returns a new string:
code = 'SGT Kim, SSG Ortiz'
code = code.replace('SGT', 'Sergeant').replace('SSG', 'Staff Sergeant')
For more than a few pairs, a dictionary and a loop read better:
swaps = {'SGT': 'Sergeant', 'SSG': 'Staff Sergeant', 'SFC': 'Sergeant First Class'}
for old, new in swaps.items():
code = code.replace(old, new)
Watch the order when one search term sits inside another. In practice, that is where chained swaps go wrong.
When to use re.sub() instead
str.replace() only finds exact text. When you need a pattern, such as any run of spaces, any digits, or a word in any capitalization, use re.sub() from the re module. The docs say re.sub() will "Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string" and that "If the pattern isn't found, string is returned unchanged." (Python docs: re.sub)
Two common jobs:
import re
clean = re.sub(r'\s+', ' ', 'Too many spaces')
unit = re.sub('alpha', 'Bravo', 'Alpha Company', flags=re.IGNORECASE)
The first collapses runs of whitespace into a single space. The second ignores case.
A simple rule of thumb: reach for str.replace() first, because it is simpler and harder to get wrong. Move to re.sub() when the text you are looking for is a pattern, not a fixed string.
When to use translate()
If you want to swap or remove many single characters, translate() is built for it. The docs say it will "Return a copy of the string in which each character has been mapped through the given translation table."
date = '2026-12-02'
digits = date.translate(str.maketrans('', '', '-'))
That strips the hyphens and leaves 20261202.
Quick reference
| You want to | Use |
|---|---|
| Swap exact text everywhere | text.replace(old, new) |
| Swap only the first few matches | text.replace(old, new, count) |
| Match a pattern or ignore case | re.sub(pattern, new, text) |
| Remove or swap single characters | text.translate(table) |
Where string work shows up on the job
Cleaning and reshaping text is everyday work for developers and data analysts: tidying names before they go into a database, fixing exports, preparing text for an AI model. If you are curious what Python work looks like as a career, read the post on the Python developer role. If you like having a test to study toward, see Python certification. And if databases interest you, SQL vs Python explains how the two work together.
Where VetBoss fits
Step Into Tech is VetBoss's AI Engineer path, in two stages. Stage 1, Full Stack Developer, covers HTML and CSS, JavaScript, Python, React JS, Certified GPT and MySQL, in live instructor-led classes with hands-on projects. Stage 2 is the apprenticeship part of the path and is not open yet: Join waiting list. See Step Into Tech.
Your next step
VetBoss is here to serve veterans, and the door is open if you have a question.
Thank you for reading, and thank you for your service. VetBoss serves veterans, and the team is glad to hear from you.
You came here to learn how to replace a string in Python. If you have served and are starting to think about what comes after service, let's map out what comes next. Our military skills translator is a place to start.
Book an eligibility call, or call 855-VET-BOSS (855-838-2677).