From fbd29773cb7814a0a0f34206efff5f21b51132f7 Mon Sep 17 00:00:00 2001 From: Vijayalaxmi Wakode Date: Fri, 4 Oct 2024 03:26:22 +0530 Subject: [PATCH 1/5] The string manipulation - replace() --- strings/replace.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 strings/replace.py diff --git a/strings/replace.py b/strings/replace.py new file mode 100644 index 000000000000..a22d70e76b97 --- /dev/null +++ b/strings/replace.py @@ -0,0 +1,21 @@ +def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str: + """ + The replace() method replaces a specified string with another specified string. + The occurence parameter can be skipped in order to consider all text. + Note: input and replace_with strings are case-sensitive. + >>> text = "One Two Two Three Four Five" + >>> string_val = string_replace(text, "Two", "Seven", 1) + >>> print(string_val) + One Seven Two Three Four Five + + >>> text = "In the morning, the cat is running behind the mouse." + >>> string_val = string_replace(text, "the", "a", 10) + >>> print(string_val) + In a morning, a cat is running behind a mouse. + """ + return text.replace(input_string, replace_with_string, occurrence) + + +if __name__ == "__main__": + from doctest import testmod + testmod() From 19db3e369ffd0c4c8a1420f6f7fb9895b5ed6a0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 21:57:56 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/replace.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/strings/replace.py b/strings/replace.py index a22d70e76b97..9b2494c3a81b 100644 --- a/strings/replace.py +++ b/strings/replace.py @@ -1,4 +1,6 @@ -def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str: +def string_replace( + text: str, input_string: str, replace_with_string: str, occurrence: int +) -> str: """ The replace() method replaces a specified string with another specified string. The occurence parameter can be skipped in order to consider all text. @@ -18,4 +20,5 @@ def string_replace(text: str, input_string: str, replace_with_string: str, occur if __name__ == "__main__": from doctest import testmod + testmod() From ce03ae68226579d676cbbe71d4f7d5104f7b94d9 Mon Sep 17 00:00:00 2001 From: Vijayalaxmi Wakode Date: Fri, 4 Oct 2024 03:35:06 +0530 Subject: [PATCH 3/5] Update replace.py --- strings/replace.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/strings/replace.py b/strings/replace.py index 9b2494c3a81b..967e251f2c69 100644 --- a/strings/replace.py +++ b/strings/replace.py @@ -1,9 +1,8 @@ -def string_replace( - text: str, input_string: str, replace_with_string: str, occurrence: int -) -> str: +def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str: """ + https://docs.python.org/3/library/stdtypes.html#str.replace The replace() method replaces a specified string with another specified string. - The occurence parameter can be skipped in order to consider all text. + The occurrence parameter can be skipped in order to consider all text. Note: input and replace_with strings are case-sensitive. >>> text = "One Two Two Three Four Five" >>> string_val = string_replace(text, "Two", "Seven", 1) @@ -20,5 +19,4 @@ def string_replace( if __name__ == "__main__": from doctest import testmod - testmod() From b4ec916773448bbd4cde08a6caa54d119984eed4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:05:57 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/replace.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/strings/replace.py b/strings/replace.py index 967e251f2c69..16ef7cbba420 100644 --- a/strings/replace.py +++ b/strings/replace.py @@ -1,4 +1,6 @@ -def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str: +def string_replace( + text: str, input_string: str, replace_with_string: str, occurrence: int +) -> str: """ https://docs.python.org/3/library/stdtypes.html#str.replace The replace() method replaces a specified string with another specified string. @@ -19,4 +21,5 @@ def string_replace(text: str, input_string: str, replace_with_string: str, occur if __name__ == "__main__": from doctest import testmod + testmod() From 9f6633c9b951bc36bf0ba68b495c18ab4c2925bf Mon Sep 17 00:00:00 2001 From: vijayalaxmi777 Date: Mon, 14 Oct 2024 18:39:06 +0000 Subject: [PATCH 5/5] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..b3891a3cbe8c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1314,6 +1314,7 @@ * [Prefix Function](strings/prefix_function.py) * [Rabin Karp](strings/rabin_karp.py) * [Remove Duplicate](strings/remove_duplicate.py) + * [Replace](strings/replace.py) * [Reverse Letters](strings/reverse_letters.py) * [Reverse Words](strings/reverse_words.py) * [Snake Case To Camel Pascal Case](strings/snake_case_to_camel_pascal_case.py)