Don't use a string, use something mutable like bytearray:

#!/usr/bin/python

s = bytearray("my dog has fleas")
for n in xrange(len(s)):
    s[n] = chr(s[n]).upper()
print s

Results in:

MY DOG HAS FLEAS

Edit:

Since this is a bytearray, you aren't (necessarily) working with characters. You're working with bytes. So this works too:

s = bytearray("\x81\x82\x83")
for n in xrange(len(s)):
    s[n] = s[n] + 1
print repr(s)

gives:

bytearray(b'\x82\x83\x84')

If you want to modify characters in a Unicode string, you'd maybe want to work with memoryview, though that doesn't support Unicode directly.

Answer from bstpierre on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_string_replace.asp
Python String replace() Method
Remove List Duplicates Reverse ... Interview Q&A Python Bootcamp Python Training ... The replace() method replaces a specified phrase with another specified phrase....
Discussions

Using replace() method
Strings are immutable. That is to say, after they've been created they cannot be modified. If you want to change them anyway, you have to make a copy that contains the changes you want. That's what .replace() does; it makes a new string that is a copy of the original with some portions of it replaced. As for why strings are immutable; there's a StackOverflow thread about that subject . On the other hand, lists are mutable (like most things). You can add, delete, modify, and rearrange its contents all you want, without having to create a new list to do it. So instead of creating a new list, .sort() just modifies the list in-place. More on reddit.com
🌐 r/learnpython
7
4
February 27, 2024
Selective replacement of matching text in a string?
I’ve having to deal with 1000’s of rows of strings that that can take any of the following form: a single word a string of words a string of words separated by a delimiter (in this case \\) I need to be able to replace all instances of matching text with another value, being sure not to ... More on discuss.python.org
🌐 discuss.python.org
16
0
January 2, 2024
Hello, is there a way to mutate strings in Python?
As a technical issue, you cannot "mutate" strings (change them in-place), so string operations always must return a new string if the result differs from the original. And besides the str.replace() method mentioned, there is str.translate() for doing multiple character remappings in one pass, and the codecs module if you really need more flexibility in customized mappings. More on reddit.com
🌐 r/learnpython
8
3
December 20, 2020
Finding and replacing specific placeholders in a text file
Once you open the file and read the contents into a string, you can use the str.replace method. More on reddit.com
🌐 r/learnpython
7
3
April 28, 2016
🌐
GeeksforGeeks
geeksforgeeks.org › python › in-place-string-modifications-in-python
In-Place String Modifications in Python - GeeksforGeeks
July 23, 2025 - However, we can simulate in-place string modifications using techniques such as string slicing and conversion to mutable data types like lists. String slicing is one of the most efficient ways for small changes.
🌐
StrataScratch
stratascratch.com › blog › how-to-replace-a-character-in-a-python-string
How to Replace a Character in a Python String - StrataScratch
October 18, 2024 - There are several ways to replace a character in a Python string. In this article, you’ll learn three main techniques and how to use them in practice.
🌐
Reddit
reddit.com › r/learnpython › using replace() method
r/learnpython on Reddit: Using replace() method
February 27, 2024 -

Hi guys, I had a quick question about the exercise I was working on.

For using the replace() method and printing the result, we have to assign it to another variable and print that new variable.

sentence = sentence.replace(‘hey’, ‘hi’) print(sentence)

But for something such as the sort method we don’t need to assign it to a new variable and just use it straight up.

list = […] list.sort() print(list)

Why is it that some methods you need to assign it to a new variable, while others work while not assigning it to a new variable and the original is changed. Thank you.

Sorry about format, I’m on mobile.

🌐
Real Python
realpython.com › replace-string-python
How to Replace a String in Python – Real Python
January 15, 2025 - In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.
🌐
Mimo
mimo.org › glossary › python › string-replace-method
Master Python's String Replace Method for Text Manipulation
replace() is a string method that replaces a string’s occurrences of a substring with another substring. The replace() method takes the old substring, the new substring, and an optional count parameter. When present, count specifies the number of occurrences to replace.
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Selective replacement of matching text in a string? - Python Help - Discussions on Python.org
January 2, 2024 - I’ve having to deal with 1000’s of rows of strings that that can take any of the following form: a single word a string of words a string of words separated by a delimiter (in this case \\) I need to be able to replace all instances of matching text with another value, being sure not to ...
🌐
Keploy
keploy.io › home › community › how to replace strings in python?
How to Replace Strings in Python: Methods, Examples & Best Practices
September 26, 2025 - Learn how to replace strings in Python using replace(), slicing, list conversion, translate(), and regex with simple examples.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-replace
Python String replace() Method - GeeksforGeeks
1 week ago - s = "Hello World! hello world!" res1 = s.replace("Hello", "Hi") res2 = s.replace("hello", "hi") print(res1) print(res2) ... Hi World! hello world! Hello World! hi world! ... Example 3: In this example, replace() is used multiple times to clean ...
🌐
freeCodeCamp
freecodecamp.org › news › python-string-replace-how-to-replace-a-character-in-a-string
Python string.replace() – How to Replace a Character in a String
September 1, 2022 - Here's an example that replaces "JavaScript" with "PHP" in a string: str = "I love JavaScript. I prefer JavaScript to Python because JavaScript looks more beautiful" new_str = str.replace("JavaScript", "PHP") print(new_str) # I love PHP.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Extract and Replace Elements That Meet the Conditions of a List of Strings in Python | note.nkmk.me
May 19, 2023 - To replace the whole element containing a specific string, use the in operator to extract it and apply conditional expressions (ternary operator), formatted as X if condition else Y. ... Use conditional expressions for the expression part of ...
🌐
YouTube
youtube.com › code master
Python Replace String Method - YouTube
Be sure to like, share and comment to show your support for our tutorials.=======================================Channel - https://goo.gl/pnKLqEPlaylist For ...
Published   October 1, 2015
Views   46K
🌐
TutorialsPoint
tutorialspoint.com › article › python-program-to-replace-the-spaces-of-a-string-with-a-specific-character
Python Program to Replace the Spaces of a String with a Specific Character
April 17, 2023 - In Python, spaces in a string can be replaced with specific characters using the replace() method. The replace() method substitutes all occurrences of a specified substring with a new substring.
🌐
DataCamp
datacamp.com › tutorial › python-string-replace
Python String Replace Tutorial | DataCamp
June 25, 2020 - Python provides you with the .replace() string method that returns a copy of the string with all the occurrences of old substring replaced with the new substring given as a parameter. It takes three arguments: the substring to replace, the new string to replace it, and an optional number that ...
🌐
iO Flood
ioflood.com › blog › python-string-replace
Python String Replace Methods | Using replace() and More
August 19, 2024 - In this case, we first convert the string to lowercase using the lower() method, then replace ‘apple’ with ‘orange’. This ensures all instances of ‘apple’, regardless of case, are replaced. These advanced techniques help you handle more complex scenarios, making the replace() method a powerful tool in your Python toolkit.
🌐
HubSpot
blog.hubspot.com › home › the hubspot website blog
Introduction to the Python Replace String Method
November 16, 2023 - HubSpot’s Website Blog covers everything you need to know to build maintain your company’s website.
🌐
Geekflare
geekflare.com › development › how to remove last character from python string?
How to Replace a Character in a String Using Python
January 20, 2025 - Slice the string from start to the last before element. buggy_name = 'GeekflareE' name = buggy_name[:-1] print(name) Let’s focus on the second line in the above code. That’s the magic line in the code.
🌐
Flexiple
flexiple.com › python › python-replace-character-in-string
Python string.replace() – How to Replace a Character in a String - Flexiple - Flexiple
March 11, 2022 - This function is part of Python's string class, allowing developers to easily transform strings for various applications such as data cleaning, formatting, and more. It operates by scanning the original string for occurrences of the specified character(s) and substituting them with the new character(s), returning a new string as the result. This method is straightforward, efficient, and indispensable for string manipulation tasks. Replacing a character in a string is a common operation in Python programming.
🌐
Python Tutorial
pythontutorial.net › home › python string methods › python string replace()
How to Use Python String replace() to Replace a Substring in a String
December 28, 2020 - In this tutorial, you'll learn how to use the Python string replace() method to replace some or all occurrences of a substring with a new substring.