Exercise 36

Write a Python program which delete all multiple spaces in a given string 's'.

Solution

# define a string with multiple spaces
s = "Hello    world!    How are you?"

# delete the multiple space in the string s
s = " ".join(s.split())

print(s)





Explanation:

  1. The program uses the split() method: to split the string into a list of words.
  2. Then, it uses join() method: to join the words back into a string with a single space between them.
  3. The output will be: "Hello world! How are you?".

 

Younes Derfoufi
my-courses.net

One thought on “Solution Exercise 36: python algorithm to delete mumtiple spaces”

Leave a Reply