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:
- The program uses the split() method: to split the string into a list of words.
- Then, it uses join() method: to join the words back into a string with a single space between them.
- The output will be: "Hello world! How are you?".
Younes Derfoufi
my-courses.net
[…] Exercice36 || Solution […]