Solving problem is about exposing yourself to as many situations as possible like Changing one character in a string and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Changing one character in a string, which can be followed any time. Take easy to follow this discuss.
What is the easiest way in Python to replace a character in a string?
text = "abcdefg"; text = "Z"; ^
Don’t modify strings.
Work with them as lists; turn them into strings only when needed.
list("Hello zorld") s ['H', 'e', 'l', 'l', 'o', ' ', 'z', 'o', 'r', 'l', 'd'] s = 'W' s ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'] "".join(s) 'Hello World's =
Python strings are immutable (i.e. they can’t be modified). There are a lot of reasons for this. Use lists until you have no choice, only then turn them into strings.
There are three ways. For the speed seekers I recommend ‘Method 2’
Given by this answer
text = 'abcdefg' new = list(text) new = 'W' ''.join(new)
Which is pretty slow compared to ‘Method 2’
timeit.timeit("text = 'abcdefg'; s = list(text); s = 'W'; ''.join(s)", number=1000000) 1.0411581993103027
Method 2 (FAST METHOD)
Given by this answer
text = 'abcdefg' text = text[:1] + 'Z' + text[2:]
Which is much faster:
timeit.timeit("text = 'abcdefg'; text = text[:1] + 'Z' + text[2:]", number=1000000) 0.34651994705200195
timeit.timeit("text = 'abcdefg'; s = bytearray(text); s = 'Z'; str(s)", number=1000000) 1.0387420654296875
new = text[:1] + 'Z' + text[2:]
Python strings are immutable, you change them by making a copy.
The easiest way to do what you want is probably:
text = "Z" + text[1:]
text[1:] returns the string in
text from position 1 to the end, positions count from 0 so ‘1’ is the second character.
You can use the same string slicing technique for any part of the string
text = text[:1] + "Z" + text[2:]
Or if the letter only appears once you can use the search and replace technique suggested
Starting with python 2.6 and python 3 you can use bytearrays which are mutable (can be changed element-wise unlike strings):
s = "abcdefg" b_s = bytearray(s) b_s = "Z" s = str(b_s) print s aZcdefg
edit: Changed str to s
edit2: As Two-Bit Alchemist mentioned in the comments, this code does not work with unicode.
Like other people have said, generally Python strings are supposed to be immutable.
However, if you are using CPython, the implementation at python.org, it is possible to use ctypes to modify the string structure in memory.
Here is an example where I use the technique to clear a string.
I mention this for the sake of completeness, and this should be your last resort as it is hackish.
This code is not mine. I couldn’t recall the site form where, I took it. Interestingly, you can use this to replace one character or more with one or more charectors.
Though this reply is very late, novices like me (anytime) might find it useful.
Change Text function.
mytext = 'Hello Zorld' mytext = mytext.replace('Z', 'W') print mytext,
Strings are immutable in Python, which means you cannot change the existing string.
But if you want to change any character in it, you could create a new string out it as follows,
def replace(s, position, character): return s[:position] + character + s[position+1:]
replace(‘King’, 1, ‘o’)
// result: Kong
Note: If you give the position value greater than the length of the string, it will append the character at the end.
replace(‘Dog’, 10, ‘s’)
// result: Dogs