def tt(w): 
    if ' ' in w: 
       print 'space' 
    else: 
       print 'no space' 

>>   tt('b ')
>> space
>>  tt('b b')
>> space
>>  tt('bb')
>> no space

I am in train, sorry for not explaining.. cannot type much..

Answer from doniyor on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_isspace.asp
Python String isspace() Method
Python Examples Python Compiler ... Bootcamp Python Training ... The isspace() method returns True if all the characters in a string are whitespaces, otherwise False....
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.6 documentation
>>> ''.isspace() False >>> ' '.isspace() True >>> '\t\n'.isspace() # TAB and BREAK LINE True >>> '\u3000'.isspace() # IDEOGRAPHIC SPACE True
Discussions

Check whitespace in a Python string - Stack Overflow
The isspace function only returns True for strings that contain nothing but whitespace. More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
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
Python isspace function - Stack Overflow
I'm having difficulty with the isspace function. Any idea why my code is wrong and how to fix it? Here is the problem: Implement the get_num_of_non_WS_characters() function. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-isspace-method
Python String isspace() Method - GeeksforGeeks
January 2, 2025 - isspace() method in Python is used to check if all characters in a string are whitespace characters. This includes spaces (' '), tabs (\t), newlines (\n), and other Unicode-defined whitespace characters.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ strings โ€บ .isspace()
Python | Strings | .isspace() | Codecademy
October 3, 2023 - The .isspace() string method takes in a string and returns True if the entire string is composed of whitespace characters, otherwise False. ... Learn to analyze and visualize data using Python and statistics.
๐ŸŒ
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.
Find elsewhere
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(' ','')
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-check-if-text-is-empty-spaces-tabs-newlines-in-python
How to check if text is โ€œemptyโ€ (spaces, tabs, newlines) in Python?
March 24, 2026 - In this article, we will explore different methods to check if text contains only whitespace characters (spaces, tabs, newlines) or is completely empty in Python. The isspace() method determines whether a string contains only whitespace characters.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ str โ€บ isspace.html
isspace โ€” Python Reference (The Right Way) 0.1 documentation
isspace ยท Edit on GitHub ยท Returns a Boolean stating whether the string contains only whitespace characters. str. isspace() bool ยท #TODO ยท For 8-bit strings, this method is locale-dependent. Returns False if string is empty.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard-library โ€บ str โ€บ isspace
Python str isspace() - Check for Whitespace | Vultr Docs
December 27, 2024 - The isspace() method in Python is a string method used to check if all characters in a string are whitespace.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ string_isspace.htm
Python String isspace() Method
The python string isspace() method is used to check whether the string consists of white space characters. This method returns true if all the characters in the input string are white space characters and there is at least one character.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python string isspace()
Python String isspace() - Be on the Right Side of Change
March 23, 2021 - Checks whether all characters are whitespaces (True or False). Minimal Example As you read over the explanations below, feel free to watch our video guide about this particular string method: Syntax and Explanation str.isspace() Checks whether all characters are whitespaces (True or False).
๐ŸŒ
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
The isspace() method is a built-in string method that returns True if all characters in the string are whitespace characters (space, tab, newline, etc.) and there is at least one character.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-pandas-series-str-isspace-method
Python | Pandas Series.str.isspace() method - GeeksforGeeks
June 6, 2022 - # importing pandas module import pandas as pd # importing numpy module import numpy as np # creating series 1 series1 = pd.Series(['a', 'b', ' ', ' c ', 'd', ' ', np.nan]) # checking for all space elements in series1 result1 = series1.str.isspace() # display print('Series 1 results:\n\n', result1)
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-isspace-in-python
What is isspace() in Python?
The isspace() method is a string function in Python that tells whether or not a string has only whitespaces.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ string โ€บ python-string-isspace
Python String isspace() method - AskPython
February 16, 2023 - The Python string isspace() method is used to check the presence of white spaces within the input string.