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
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
Replacing console output
You should be able to do this by using a carriage return. I.e. instead of outputting a newline character \n at the end of the line, you can use a \r, which takes you back to the beginning of the line. In Python 3 you can do the following: import time for i in range(10): print('Attempt {} of 10', end='\r') time.sleep(1) # just so we can actually see the change More on reddit.com
๐ŸŒ r/Python
27
37
July 29, 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.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ replace
Python String replace()
If the old substring is not found, it returns a copy of the original string. ... # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt')) song = 'Let it be, let it be, let it be, let it be'
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ string_replace.htm
Python String replace() Method
The Python String replace() method replaces all occurrences of one substring in a string with another substring. This method is used to create another string by replacing some parts of the original string, whose gist might remain unmodified.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-do-i-modify-a-string-in-place-in-python
How do I modify a string in place in Python?
Unfortunately, you cannot modify a string in place because strings are immutable. Simply create a new string from the several parts you want to gather it from. Though, if you still need an object with the ability to modify in-place unicode data, you
๐ŸŒ
Python Central
pythoncentral.io โ€บ pythons-string-replace-method-replacing-python-strings
Python's string.replace() Method - Replacing Python Strings | Python Central
December 21, 2013 - Replacing Python Strings Often you'll have a string (str object), where you will want to modify the contents by replacing one piece of text with another. In Python, everything is an object - including strings. This includes the str object. Luckily, Python's string module comes with a replace() ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-replace
Python String replace() | DigitalOcean
August 4, 2022 - Python string replace() function is used to create a string by replacing some parts of another string. ... The original string remains unmodified. The new string is a copy of the original string with all occurrences of substring old replaced by new. If optional argument count is provided, then ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-string-replace
Python String replace() Method - GeeksforGeeks
October 28, 2024 - Hi Python!". Note: Since replace() creates a new string, the original string remains unchanged. ... count (optional): Specifies the maximum number of replacements to perform. If omitted, all occurrences are replaced. Returns a new string with the specified replacements made. The original string remains unchanged since strings in Python are immutable.
๐ŸŒ
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.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-string-replace-function-in-python-for-substring-substitution
Python String.Replace() โ€“ Function in Python for Substring Substitution
January 24, 2022 - When using the .replace() Python method, you are able to replace every instance of one specific character with a new one. You can even replace a whole string of text with a new line of text that you specify.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Replace Strings in Python: replace(), translate(), and Regex | note.nkmk.me
May 4, 2025 - Although Python does not have a built-in method to replace substrings at specific positions, you can achieve this by splitting the string with slicing and concatenating the parts with the replacement string.
๐ŸŒ
Devcuriosity
devcuriosity.com โ€บ manual โ€บ details โ€บ python-replace-function
Python - replace() function with examples. Replace parts of a string.
Please note that function replace() needs to be called on your string and also assigned to a new variable. It isn't possible to replace a string in-place (strings are immutable).
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ python string replace() | a detailed explanation!
Python String Replace() | A Detailed Explanation!
February 21, 2025 - This function, when applied to ... substring. The replace() method in Python is used to replace occurrences of a specified substring with another substring in a given string....
๐ŸŒ
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.