Python - Ways to remove multiple empty spaces from string List Last Updated : 28 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.Using str.split() and str.join()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. Python li = ["Hello world", " Python is great ", " Extra spaces here "] # Remove multiple spaces in each string using split and join c = [' '.join(string.split()) for string in li] print(c) Output['Hello world', 'Python is great', 'Extra spaces here'] Explanation:We split each string in the list into words using split(), which automatically removes any extra spaces and then joins them back into a single string with a single space between words using join().This process ensures that all strings in the list are cleaned of unnecessary spaces, leaving only one space between words.Using re.sub() with Regular ExpressionsUsing 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. Python import re s = ["Hello world", " Python is great ", " Extra spaces here "] # Remove multiple spaces using regex li = [re.sub(r'\s+', ' ', string.strip()) for string in s] print(li) Output['Hello world', 'Python is great', 'Extra spaces here'] Explanation:re.sub(r'\s+', ' ', string.strip()) replaces consecutive spaces (matched by \s+) with a single space, and strip() removes any leading or trailing spaces from each string.List comprehension applies this operation to each string in the input list, s, resulting in a cleaned list of strings with only single spaces between words.Using str.replace() in a LoopUsing 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. Python s = ["Hello world", " Python is great ", " Extra spaces here "] # Remove multiple spaces using replace in a loop b = [string.replace(' ', ' ') for string in s] # Repeat the replace step until no double spaces exist a = [string.replace(' ', ' ') for string in b] print(a) Output['Hello world', ' Python is great ', ' Extra spaces here '] Explanation:This method uses a list comprehension to iterate over each string in the list and replaces double spaces (' ') with single spaces (' ').Process is repeated to ensure all occurrences of multiple spaces are removed, leaving only single spaces between words. Comment More infoAdvertise with us Next Article Python | Remove unwanted spaces from string M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python string-programs Practice Tags : python Similar Reads Remove spaces from a string in Python Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so:Using replace() methodTo remove all spaces from a s 2 min read Python | Ways to remove numeric digits from given string In Python, we can remove numeric digits from a string in multiple ways. For example, if we have a string like "Geeks123" and we want to remove the numbers to get "Geeks", we can use different methods to accomplish this. We can do this by using techniques such as list comprehension, regular expressio 3 min read Python | Remove unwanted spaces from string Sometimes, while working with strings, we may have situations in which we might have more than 1 spaces between intermediate words in strings that are mostly unwanted. This type of situation can occur in web development and often needs rectification. Let's discuss certain ways in which this task can 4 min read Remove Multiple Characters from a String in Python Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Letâs explore the different ways to achieve this in det 2 min read Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing 3 min read Python | Remove all digits from a list of strings The problem is about removing all numeric digits from each string in a given list of strings. We are provided with a list where each element is a string and the task is to remove any digits (0-9) from each string, leaving only the non-digit characters. In this article, we'll explore multiple methods 4 min read Like