🌐
W3Schools
w3schools.com › python › ref_string_isspace.asp
Python String isspace() Method
Remove List Duplicates Reverse ... Bootcamp Python Training ... The isspace() method returns True if all the characters in a string are whitespaces, otherwise False....
🌐
Programiz
programiz.com › python-programming › methods › string › isspace
Python String isspace()
s = '\t \n' if s.isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters') s = '2+2 = 4' if s.isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters.') Output · All whitespace characters Contains non-whitespace characters · Previous Tutorial: Python String isprintable() Next Tutorial: Python String istitle() Share on: Did you find this article helpful?
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › isspace
Python str isspace() - Check for Whitespace | Vultr Docs
December 27, 2024 - By using list comprehension combined with isspace(), this code filters out any strings that consist solely of whitespace, resulting in a cleaned list of text data. The isspace() method in Python is a highly effective tool for identifying and ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-for-whitespaces-in-a-python-lists
How to check for whitespaces in a Python Lists?
August 29, 2023 - # The List data structure is ... it contains whitespace") else: print("no, it does not whitespace") ... The Python program is used to check for the whitespaces in the given list data structure....
🌐
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 ·
🌐
Javatpoint
javatpoint.com › python-string-isspace-method
Python String | isspace() method with Examples - Javatpoint
Python String isspace() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.
🌐
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 - It would be extremely helpful and ... The Python isspace() String function examines a string for whitespace characters and returns True only if the string contains all spacing characters, i.e....
🌐
GeeksforGeeks
geeksforgeeks.org › 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.
🌐
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.
Find elsewhere
🌐
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
🌐
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
🌐
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) Previous isprintable() Next istitle() Python Tutorial · What is Python? What do you use Python for? What can you do with Python? Why use Python? Hello World! Variables and Data Types · Lists ·
🌐
Codecademy
codecademy.com › docs › python › strings › .isspace()
Python | Strings | .isspace() | Codecademy
October 3, 2023 - Checks if all the characters in a string are whitespace characters.
🌐
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 - text1 = " " # Single space text2 = " Hello" # Spaces with text text3 = "\n\t" # Newline and tab text4 = "" # Empty string print(f"'{text1}' contains only whitespace:", text1.isspace()) print(f"'{text2}' contains only whitespace:", text2.isspace()) print(f"'{text3}' contains only whitespace:", text3.isspace()) print(f"'{text4}' contains only whitespace:", text4.isspace())
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-check-for-whitespace-in-list
Python | Check for Whitespace in List - GeeksforGeeks
April 13, 2023 - test_list = ["Geeks forGeeks", "is", "best"] print("The original list is : " + str(test_list)) res = False for ele in test_list: if any(c.isspace() for c in ele): res = True break print("Does any string contain spaces ? " + str(res)) #This code is contributed by Jyothi pinjala. Output · The original list is : ['Geeks forGeeks', 'is', 'best'] Does any string contain spaces ? True · Time complexity: O(n*m) Auxiliary space: O(1) Comment · Python Fundamentals ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-series-str-isspace-method
Python | Pandas Series.str.isspace() method - GeeksforGeeks
June 6, 2022 - Syntax: Series.str.isspace() Return type: Boolean Series · Example #1: In this example, a series is made from a python list using Pandas .Series() method. The series is by default a string series with some elements as All-space.
🌐
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).
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(' ','')