Does the character being replaced in .replace() have to be nonempty?


In the context of this exercise, does the character being replaced in the .replace() method have to be nonempty?


No, the character being replaced in the .replace() method does not have to be a nonempty string, and can be the empty string ''.

When providing an empty string as the argument for the character to be replaced, this will essentially replace every part between characters in the string. One way you can look at this, is as though every nonempty character has two empty spaces around it, which can overlap with their adjacent characters.

Here is an example that might help to illustrate this more clearly

empty_string = ''
replaced = empty_string.replace('', '!!!')
print(replaced) # !!!

example_string = "code"
result = example_string.replace('', '==')
print(result) # ==c==o==d==e==


if in this example:

instead of '' we used ' ' as a character to be replaced, what would happen? Would it give us some kind of error because we don’t have any whitespaces in our string (“code”)?

Thank you.


If in s.replace(old, new), old is not present in s, the function simply returns s.

s1 = 'abc'
s2 = s1.replace('b', 'x')
s3 = s1.replace('z', 'x')

# Output:


>>> 'code'.replace('', ' ')
' c o d e '

As we can see there is a preponderance of empty strings through the string, code so the method does what it knows how to do. It replaces every instance with a space.

It does not know how to replace something that is not there so it simply ignores it and does nothing.


How can I count empty spaces ‘’ in a string I’m trying something like this :

my = "abcdef"

for i in my:
  if i == '':

specially how replace method detect empty places in given string


The for loop will only iterate over members of the sequence (characters). There are no characters in between. What replace does is not the same. It replaces all instances of nothing with a space (in the above). We would need to know the inner workings of the method to understand how it does this, but that is not important. We know that we can insert a character or characters between each character in a string.

We can demonstrate similar behavior with the str.join() method.

>>> ' '.join('abcdef')
'a b c d e f'

The how of it is a bit of a mystery, but the logic makes sense.

Bottom line, use only code that we understand and can predict the behavior of so we know what to expect, and how to plan it.


.replace can take 3 arguments i.e,

word = "Hello Hello Hello"
re = word.replace("H", "J", 2)

here 2 is the number of times you want the word or character to be replaced

You have just mentioned 2… just wanted to say that


Which if anybody reads the documentation they will be quick to learn. However, it is moot if we want global replacement, which is the default when we do not specify a maximum count.

1 Like

why this code replace five instances though we have only four letters. please explain how does it works.
and one more thing how does this code append the list, given bellow(explain step-wise process):

list = []
list += "Hello"

How many empty strings are there in a four letter word? Answer: Five, as is clearly demonstrated. Can we say that the empty spaces exist only between the letters? Why not at the ends, also. Empty spaces are essentially undefined, but if we must give them some definition it would follow there is an empty space on both sides of a single letter, hence the ones on the ends.

>>> lst = []
>>> lst += "hello"
>>> lst
['h', 'e', 'l', 'l', 'o']

It will append the list with a sequence of single letter strings the same way list.extend() does. To keep the string intact, it has to be a list, tuple or set object.

>>> lst = []
>>> lst += ["hello"]
>>> lst

That’s fascinating .