You want non whitespace, so you should use not

def get_num_of_non_WS_characters(s):
    count = 0
    for char in s:
        if not char.isspace():
            count += 1
    return count

>>> get_num_of_non_WS_characters('hello')
5
>>> get_num_of_non_WS_characters('hello  ')
5

For completeness, this could be done more succinctly using a generator expression

def get_num_of_non_WS_characters(s):
    return sum(1 for char in s if not char.isspace())
Answer from Cory Kramer on Stack Overflow
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ isspace
Python String isspace()
The isspace() method returns True if there are only whitespace characters in the string. If not, it return False.
Discussions

Isspace function in python - Stack Overflow
I'm having trouble trying to execute this code, I want the user to input a value, the program checks if that value is a string then it returns the length. If the value contains whitespaces the pro... More on stackoverflow.com
๐ŸŒ stackoverflow.com
STRING METHOD FOR SPACES
Use the in operator. if " " in email: ... More on reddit.com
๐ŸŒ r/learnpython
6
1
August 16, 2023
Check whitespace in a Python string - Stack Overflow
Please note: isspace does not return true for None or '' . More on stackoverflow.com
๐ŸŒ stackoverflow.com
using isspace() to check if it is a single word
That method checks if all characters in a string are whitespace. Something like check: bool = not ' ' in x print(check) should work. More on reddit.com
๐ŸŒ r/learnpython
14
1
March 6, 2023
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_isspace.asp
Python String isspace() Method
txt = " s " x = txt.isspace() print(x) Try it Yourself ยป ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ string โ€บ python-string-isspace
Python String isspace() method - AskPython
February 16, 2023 - Note: Series.str.isspace() method works only for string type elements.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-isspace-method
Python String isspace() Method - GeeksforGeeks
January 2, 2025 - The isspace() method does not take any parameters. Returns True if all characters in the string are whitespace. Returns False if the string contains one or more non-whitespace characters or is empty.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ string_isspace.htm
Python String isspace() Method
The following is an example of the python string isspace() method. str = " " result=str.isspace() print("Are all the characters of the string spaces?", result) On executing the above program, the following output is generated - ... The alphabets do not come under white space characters.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ str โ€บ isspace.html
isspace โ€” Python Reference (The Right Way) 0.1 documentation
>>> ''.isspace() False >>> 'abc123'.isspace() False >>> 'abc'.isspace() False >>> '123'.isspace() False >>> 'Abc'.isspace() False >>> '!@#'.isspace() False >>> ' '.isspace() True >>> 'ABC'.isspace() False
Find elsewhere
๐ŸŒ
Toppr
toppr.com โ€บ guides โ€บ python-guide โ€บ references โ€บ methods-and-functions โ€บ methods โ€บ string โ€บ isspace โ€บ python-string-isspace
Python isspace() function | Why do we use Python String isspace()? |
August 26, 2021 - One of the string inputs in the above program consists of a combination of whitespace and non-whitespace characters. For this situation also, the isspace() function returns a False value. Python isspace() also supports Unicode characters which qualify as whitespace characters in Python.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ isspace python function | check if a string has space
isspace() Python function | Check if a string has space - EyeHunts
July 16, 2021 - Python String isspace() will return a boolean value, if there is space only then true else return False ... True if all characters in the string are whitespace (space) characters else False. ... If all the characters in the text are whitespaces it will return true. ... Output: It will work but the return value will be false. ... Do comment if you have any questions and suggestions. Note: IDE: PyCharm 2020.1 (Community Edition) macOS 10.15.4 Python 3.7 All Python Examples are in Python 3, so it may change its different from python 2 or upgraded versions.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard-library โ€บ str โ€บ isspace
Python str isspace() - Check for Whitespace | Vultr Docs
December 27, 2024 - Use the isspace() method to evaluate the string. ... In this example, mixed_str contains words along with spaces. Since not all characters are whitespace, isspace() returns False.
๐ŸŒ
Python
bugs.python.org โ€บ issue18236
Issue 18236: str.isspace should use the Unicode White_Space property - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide ยท This issue has been migrated to GitHub: https://github.com/python/cpython/issues/62436
๐ŸŒ
Javatpoint
javatpoint.com โ€บ python-string-isspace-method
Python String | isspace() method with Examples - Javatpoint
Python isspace() method is used to check space in the string. It returna true if there are only whitespace characters in the string. Otherwise it returns false. Space, newline, and tabs etc are known as whitespace characters and are defined in the Unicode character database as Other or Separator ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-check-if-a-string-is-whitespace-in-python-559582
How to Check If a String Is Whitespace in Python | LabEx
As you can see, isspace() returns True only for strings that contain only whitespace characters and are not empty. In this step, you will learn to differentiate between strings containing only whitespace and empty strings in Python.
๐ŸŒ
Jobtensor
jobtensor.com โ€บ Tutorial โ€บ Python โ€บ en โ€บ String-Methods-isspace
Python String isspace(), Definition, Syntax, Parameters, Examples | jobtensor
testStr = " " result = testStr.isspace() print(result) # another example testStr = "Welcome to the Python tutorials" result = testStr.isspace() print(result)
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-string-isspace
Python isspace Function
March 23, 2025 - The Python isspace function returns True if all characters in a given string are whitespace or empty. Otherwise, the isspace returns False.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ python-string-isspace-method
Linux Hint โ€“ Linux Hint
October 24, 2022 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
Top answer
1 of 4
1

I don't think the str.isspace, str.isalpha and str.isdigit methods do what you expect them to do. To start with, they all test if all the characters in the string you enter are of the type that is described in their name. Your code seems to be expecting them to be return True if any of the characters match. That is, if there are any spaces, you want to remove them and show the two lengths, before and after.

There's no single string method that will do that test for you in Python. You could use regular expressions (which are more powerful, but much more complicated), or you could write some slightly more elaborate code to do the test. I'd suggest using the any function, and passing it a generator expression that calls the method you want on each character in the string.

if any(c.isspace() for c in user_str):
    ...

This may not be exactly what you want for all of your tests. The desired logic of your code is not entirely obvious, as there are a number of corner cases that your output doesn't specifically address. Is a string that contains both letters and numbers valid? How about one that has spaces in between numbers, but no letters at all? You may need to reorder the conditions of your if/elif/else statements so that they match what you intend.

I'd also note that the variable name you used for user input, new_length, is very misleading. It's not a length, its the string you want to measure the length of! It's a lot easier to make logic errors about variables that have misleading or unclear variable names, so taking time to go back and reconsider names you chose earlier is sometimes a good idea, as it can improve the clarity of your code a lot! Descriptive variable names are good, but it's a tradeoff between clarity and brevity, as long names are tedious to type (and prone to typos). They also can lead to line length issues, which can make it less convenient to see all your code on your editor screen at once.

2 of 4
0

You can use this function to check if the input string contains a number:

def hasNumbers(inputString):
        return any(char.isdigit() for char in inputString)

It returns true if there is a number and false if there is not.

As for the whitespaces you can ommit isspace(). Using replace() alone will do the job, even if there are no whitespaces.

stri='jshsb sjhsvs jwjjs'
stri=stri.replace(' ','')
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-string-isspace-method
Python String isspace() Method - Learn By Example
April 20, 2020 - # Check if the string contains only whitespace characters S = ' ' x = S.isspace() print(x) # Prints True S = ' a' x = S.isspace() print(x) # Prints False
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ string method for spaces
r/learnpython on Reddit: STRING METHOD FOR SPACES
August 16, 2023 -

Is there any string method that checks for spaces? I know isspace() checks for them but it only gives a true if the string only includes white spaces. I'm looking for one that will prince a false if there is any spaces. I'm using these for a project that splices emails so isalpha() wont work cos of that @ and numeric symbols

here is the code

print("Your email may not contain any spaces.")

email = input("Enter your email: ")

index = email.index("@") # this method will return a number

print(index)

username = email[:index]

domain = email[index:]

print(f"your username is {username} and domain is {domain}")