Skip to content

Commit fbd2977

Browse files
The string manipulation - replace()
1 parent 40f65e8 commit fbd2977

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: strings/replace.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str:
2+
"""
3+
The replace() method replaces a specified string with another specified string.
4+
The occurence parameter can be skipped in order to consider all text.
5+
Note: input and replace_with strings are case-sensitive.
6+
>>> text = "One Two Two Three Four Five"
7+
>>> string_val = string_replace(text, "Two", "Seven", 1)
8+
>>> print(string_val)
9+
One Seven Two Three Four Five
10+
11+
>>> text = "In the morning, the cat is running behind the mouse."
12+
>>> string_val = string_replace(text, "the", "a", 10)
13+
>>> print(string_val)
14+
In a morning, a cat is running behind a mouse.
15+
"""
16+
return text.replace(input_string, replace_with_string, occurrence)
17+
18+
19+
if __name__ == "__main__":
20+
from doctest import testmod
21+
testmod()

0 commit comments

Comments
 (0)