List

Change a character in a string

Let's say you have the following string (which just so happens to be a URL, which isn't important):

https://www.iclr.co.ui/ic/1981007549

There's a problem with this URL, the .uk component of the address has mistakenly been entered as .ui. Let's fix this with Python.

First, we take the URL and pass it as a string into the list() function.

s = list('https://www.iclr.co.ui/ic/1981007549)

Then, we identify where the offending character (in this case, i) appears (it's 22nd character). Bearing in mind that Python is zero-indexed, we can target it as the 21st item in the list and change the value to k like so:

s[21] = 'k'

No that the character has been corrected, we can join() the list back together to form the corrected string:

s = "".join(s)