简体   繁体   中英

Python replace function outputs different results

s="|0000011111111|"
print s.replace(s[9:12],'   ')

...output is |00000 11|

s="|0000012345678|"
print s.replace(s[9:12],'   ')

...output is |00000123 78|

I only want to replace s[9:12] to three spaces, why is the first output incorrect?

In the first case s[9:12] is '111' . You code replaces every non-overlapping instance of '111' with three spaces.

str.replace() will replace all occurrences of the string you pass in as the first argument. It doesn't matter here that the first argument here is created from a slice, the str.replace() method is not given any information as to where that string came from. Instead, the str.replace() method starts at position 0 and searches forward, it doesn't matter where the substring is found in the input string.

So you are really just doing this:

string_to_replace = s[9:12]
s.replace(string_to_replace, '   ')

or even just

s.replace('111', '   ')

These all have the exact same effect.

Your second example only worked because the s[9:12] string value is unique in s ; there is only one occurrence to replace, at exactly the right location.

Because s[9:12] is the string '111' , you asked Python to replace all occurances of that string in s with spaces, and there are two such sequences to replace.

You are far better off explicitly slicing the input string:

s = s[:9] + '   ' + s[12:]

That takes all text before the part you want to replace and all the text after , and puts three spaces in between those two substrings.

If you call s.replace(s[9:12], ' ') then Python does not interpret this as " replace the characters at index 9 to index 12 with empty characters ". It first evaluates s[9:12] to '111' . And then it will start replacing groups of three ones with spaces, regardless where these are found.

In case you want to replace the characters at a specific index, you can use:

print s[:9] + '   ' + s[12:]

Here we take the string until (without) index 9, followed by three spaces, followed by the characters starting from index 12.

Note that in case the original string does not contain 12 or more characters, we will still add three spaces to it.

According to the Python documentation :

str.replace(old, new[, count])

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

So replace() does not replace at a position, it replaces all occurrences of a substring found inside your string by another given string.

  • In your first example, s[9:12] is equal to 111 , so all the occurrences of this substring are replaced.

  • In your second "working" example, you're lucky because s[9:12] is equal to 456 and since there is only one occurrence of this substring in s at s[9:12] , it is correctly replaced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM