Python - Avoid Spaces in string length Last Updated : 14 Jan, 2025 Comments Improve Suggest changes Like Article Like Report When working with strings in Python, we may sometimes need to calculate the length of a string excluding spaces. The presence of spaces can skew the length when we're only interested in the number of non-space characters. Let's explore different methods to find the length of a string while ignoring spaces.Using replace()replace() function allows us to remove all spaces from the string before calculating the length. This method is simple and efficient for removing spaces and counting the characters left. Python s = "geeks for geeks" res = len(s.replace(" ", "")) print(res) Output13 Explanation:We use the replace() function to remove all spaces in the string by replacing them with an empty string.Then, we use len() to calculate the length of the string without spaces.Let's explore some more methods and see how we can avoid spaces in string length.Table of ContentUsing for loopUsing list comprehensionUsing filter()Using regular expressionsUsing for loopWe can also iterate over the string and count each character that is not a space. This method gives us more control over the process but requires more code. Python s = "geeks for geeks" res = 0 for c in s: if c != " ": res += 1 print(res) Output13 Explanation:We initialize the result variable res to 0.The for loop iterates over each character in the string, and if the character is not a space, we increment the res variable.Using list comprehensionA more concise approach involves using list comprehension to filter out spaces and then using the len() function to get the length of the resulting list. Python s = "geeks for geeks" res = len([c for c in s if c != " "]) print(res) Output13 Explanation:A list comprehension is used to generate a list of characters that are not spaces.We then calculate the length of this list using len().Using filter()The filter() function is another way to remove spaces from the string before calculating the length. It allows us to filter out non-space characters and then use len() to count them. Python s = "geeks for geeks" res = len(list(filter(lambda c: c != " ", s))) print(res) Output13 Explanation:We use filter() with a lambda function to filter out spaces from the string.The resulting filter object is converted to a list, and we then use len() to find its length.This method is functional but less efficient than using replace().Using regular expressionsWe can use the re.sub() function to remove all spaces before calculating the length. Python import re s = "geeks for geeks" res = len(re.sub(r" ", "", s)) print(res) Output13 Explanation:We use the re.sub() function to replace all spaces with an empty string.Then, we calculate the length of the resulting string using len(). Comment More infoAdvertise with us Next Article Python - Avoid Spaces in string length M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Find Length of String in Python In this article, we will learn how to find length of a string. Using the built-in function len() is the most efficient method. It returns the number of items in a container. Pythona = "geeks" print(len(a)) Output5 Using for loop and 'in' operatorA string can be iterated over, directly in a for loop. 2 min read Python - All possible space joins in String Sometimes, while working with Python Strings, we can have a problem in which we need to construct strings with a single space at every possible word ending. This kind of application can occur in domains in which we need to perform testing. Let us discuss certain ways in which this task can be perfor 8 min read How to Pad a String to a Fixed Length with Spaces in Python Padding strings is useful for formatting data, aligning text, or preparing strings for output. The simplest and most common way to pad a string in Python is by using the built-in ljust() and rjust() methods.Using ljust() and rjust() MethodsThese methods add spaces to the left or right of the string 2 min read Python - Check for spaces in string Sometimes, while working with strings in Python, we need to determine if a string contains any spaces. This is a simple problem where we need to check for the presence of space characters within the string. Let's discuss different methods to solve this problem.Using 'in' operator'in' operator is one 3 min read Python - String Split including spaces String splitting, including spaces refers to breaking a string into parts while keeping spaces as separate elements in the result.Using regular expressions (Most Efficient)re.split() function allows us to split a string based on a custom pattern. We can use it to split the string while capturing the 3 min read Like