From df1ab3bbfe6f0dce07d77722a34d655aca5f5b82 Mon Sep 17 00:00:00 2001 From: stathisraf <61472058+stathisraf@users.noreply.github.com> Date: Tue, 11 Oct 2022 15:39:45 +0300 Subject: [PATCH] Create replace whitespaces with underscores.py --- .../replace whitespaces with underscores.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Problem Solving/replace whitespaces with underscores.py diff --git a/Problem Solving/replace whitespaces with underscores.py b/Problem Solving/replace whitespaces with underscores.py new file mode 100644 index 0000000..e37da41 --- /dev/null +++ b/Problem Solving/replace whitespaces with underscores.py @@ -0,0 +1,16 @@ +import re + + +Input = input("Please enter a string: ") +def replace_white_spaces_with_underscores(input_string): + list_input = list(input_string) + final_string = " " + white_spaces = [] + for match in re.finditer('\s', input_string): + white_spaces.append(match.start()) + + for i in range(len(white_spaces)): + list_input[white_spaces[i]] = "_" + return final_string.join(list_input) + +print(replace_white_spaces_with_underscores(Input))