官术网_书友最值得收藏!

The replace( ) method

The replace method is used to perform string replacement. It returns a new string with the appropriate replacements. The first argument to the replace method is the string or character to be replaced within the string, while the second argument is the string or character with which it is to be replaced:

In the preceding example, we can see that the ! from the original string is replaced by @ and a new string with the replacement is returned. It should be noted that these changes were not actually made to the original string, but instead a new string was returned with the appropriate changes. This can be verified in the following line, where we print the original string and the old unchanged value, Welcome to python strings !, is printed. The reason behind this is that strings in Python are immutable, just like they are in Java. This means that once a string is declared, it can't usually be modified. This isn't always the case, however. Let's try to change the string and this time try and catch the modifications in the originally declared string, my_str, as follows:

In the preceding code, we were able to modify the original string, as we got the newly returned string from the replace method in our earlier declared string, my_str. This might sound contradictory to what we said previously. Let's take a look at how this works by looking at what happens behind the scenes before and after we call the replace method:

After replacing the ! with @, this will look as follows:

It can be seen in the preceding two illustrations that before the replace method was called, the my_str string reference pointed toward the actual object that contained an !. Once the replace() method returned a new string and we updated the existing string variable with the newly returned object, the older memory object was not overwritten, but instead a new one was created. The program reference now points toward the newly created object. The earlier object is in memory and doesn't have any references pointing toward it. This will be cleaned up by the garbage collector at a later stage.

Another thing we can do is try and change any character in any position of the original string. We have already seen that the string characters can be accessed by their index, but if we try to update or change a character at any specific index, an exception will be thrown and the operation will not be permitted, as shown in the following screenshot:

By default, the replace() method replaces all the occurrences of the replacement string within the target string. If we only want to replace one or two occurrences of something within the target string, however, we can pass a third argument to the replace() method and specify the number of replacement occurrences that we want to have. Let's say we have the following string:

If we just want the first occurrence of the ! character to be @ and we want the rest to be the same, this can be achieved as follows:

主站蜘蛛池模板: 格尔木市| 台南市| 石首市| 辉县市| 尖扎县| 皋兰县| 介休市| 呼和浩特市| 土默特左旗| 黄平县| 达尔| 康保县| 康保县| 喀什市| 三都| 哈密市| 大厂| 哈密市| 新邵县| 广德县| 镇安县| 米易县| 都兰县| 惠东县| 庆安县| 康乐县| 巴东县| 阜宁县| 大化| 荔波县| 辉南县| 凤翔县| 抚州市| 灵丘县| 兴安县| 兴义市| 青冈县| 三门峡市| 河津市| 宁都县| 秦安县|