![]() |
VOOZH | about |
We are given a list of strings li =["Hello world", " Python is great ", " Extra spaces here "] we need to remove all extra spaces from the strings so that the output becomes ["Hello world", "Python is great' , "Extra spaces here"] .This can be done using methods like split() and join(), regular expressions or list comprehensions.
str.split() divides a string into substrings based on a delimiter while str.join() combines a list of substrings into a single string with a specified separator.
['Hello world', 'Python is great', 'Extra spaces here']
Explanation:
Using re.sub() with regular expressions allows you to efficiently replace multiple spaces in a string with a single space. It’s a powerful method for cleaning up text data by handling patterns of unwanted whitespace.
['Hello world', 'Python is great', 'Extra spaces here']
Explanation:
Using str.replace() in a loop allows for replacing specific substrings in each string of a list or text. This method iterates through the strings applying the replacement operation for each occurrence of the target substring.
['Hello world', ' Python is great ', ' Extra spaces here ']
Explanation: