1

The below string replace code prints "XXaXXbXXcXX1XX2XX3XX". I know there is a reason behind it but having hard time finding it. Can someone please explain why?

if __name__ == '__main__':
    match = ""
    replace = "XX"
    strr = "abc123"
    print strr.replace(match, replace)

2 Answers 2

3

It's replacing the empty string between each pair of characters with XX.

Sign up to request clarification or add additional context in comments.

1 Comment

And the empty strings at the beginning and end of the string.
2

You replace every "" (empty string) by "XX". Python consider that between two character there is a empty string!!! And before the first character and after the last.
It's just that!

1 Comment

'It's just that'. Exactly the confirmation I wanted to hear. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.