🌐
cppreference.com
en.cppreference.com › c › string › byte › isspace
isspace - cppreference.com
#include <ctype.h> #include <limits.h> #include <stdio.h> int main(void) { for (int ndx = 0; ndx <= UCHAR_MAX; ++ndx) if (isspace(ndx)) printf("0xx ", ndx); }
🌐
GeeksforGeeks
geeksforgeeks.org › c language › isspace-in-c
isspace() in C - GeeksforGeeks
September 13, 2024 - The isspace() function returns an integer value that tells whether the passed parameter is a whitespace character or not.
🌐
Programiz
programiz.com › c-programming › library-function › ctype.h › isspace
C isspace() - C Standard Library
In C programming, isspace( ) checks whether a character is white-space character or not. If a character passed to isspace( ) is white-space character, it returns non-zero integer if not it returns 0.
🌐
W3Schools
w3schools.com › c › ref_ctype_isspace.php
C ctype isspace() Function
The isspace() function returns a non-zero value (equivalent to boolean true) if a character is a whitespace character.
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009696599 › functions › isspace.html
isspace
The isspace() function shall return non-zero if c is a white-space character; otherwise, it shall return 0.
🌐
SAS
support.sas.com › documentation › onlinedoc › sasc › doc700 › html › lr1 › z2055475.htm
Function Descriptions : isspace
isspace tests an integer value c to determine whether it is white space; that is, a blank, tab, new line, carriage return, form feed, or vertical tab character.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re129.html
isspace - C in a Nutshell [Book]
December 16, 2005 - The function isspace() tests whether its character argument produces whitespace rather than a glyph when printed—such as a space, tabulator, newline, or the like.
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_isspace.htm
C Library - isspace() function
#include <stdio.h> #include <ctype.h> int main () { int var1 = 't'; int var2 = '1'; int var3 = ' '; if( isspace(var1) ) { printf("var1 = |%c| is a white-space character\n", var1 ); } else { printf("var1 = |%c| is not a white-space character\n", var1 ); } if( isspace(var2) ) { printf("var2 = |%c| is a white-space character\n", var2 ); } else { printf("var2 = |%c| is not a white-space character\n", var2 ); } if( isspace(var3) ) { printf("var3 = |%c| is a white-space character\n", var3 ); } else { printf("var3 = |%c| is not a white-space character\n", var3 ); } return(0); } ... var1 = |t| is not a white-space character var2 = |1| is not a white-space character var3 = | | is a white-space character · This example checks if the newline character ('\n') is a whitespace character. Since it is, the function returns true.
Find elsewhere
🌐
W3Schools
w3schools.com › PYTHON › ref_string_isspace.asp
Python String isspace() Method
The isspace() method returns True if all the characters in a string are whitespaces, otherwise False.
🌐
MathWorks
la.mathworks.com › matlab › language fundamentals › data types › characters and strings
isspace - Determine which characters are space characters - MATLAB
TF = isspace(A) returns a logical array TF. If A is a character array or string scalar, then the elements of TF are logical 1 (true) where corresponding characters in A are space characters, and logical 0 (false) elsewhere.
🌐
YouTube
m.youtube.com › live › 2uws6w63wUU
isspace() Function | C Programming Tutorial
Share your videos with friends, family, and the world
Published   April 26, 2023
🌐
LabEx
labex.io › tutorials › numpy-isspace-function-86464
Exploring NumPy's Isspace Function | LabEx
The isspace() function returns an output array of Boolean values with True and False values corresponding to every string element based on whether the string has only whitespace characters or not.
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(' ','')
🌐
Vultr
docs.vultr.com › clang › standard-library › ctype-h › isspace
C ctype.h isspace() - Check for Whitespace Character | Vultr Docs
September 27, 2024 - You will explore practical examples ... in your C programs. isspace() is a standard library function that checks for several types of whitespace characters, including space ' ', form feed '\f', newline '\n', carriage return '\r', ...
🌐
Arduino
docs.arduino.cc › language-reference › en › functions › characters › isSpace
isSpace() | Arduino Documentation
April 23, 2025 - Use the following function to evaluate a char variable: isSpace(thisChar) The function admits the following parameter: thisChar: char variable to evaluate. Allowed data types: char. The function returns · true if the evaluated input variable is a white-space character.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › isspace-iswspace-isspace-l-iswspace-l
isspace, iswspace, _isspace_l, _iswspace_l | Microsoft Learn
Each of these routines returns nonzero if c is a particular representation of a space character. isspace returns a nonzero value if c is a white-space character (0x09 - 0x0D or 0x20).
🌐
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.
🌐
Programiz
programiz.com › cpp-programming › library-function › cctype › isspace
C++ isspace() - C++ Standard Library
The isspace() function in C++ checks if the given character is a whitespace character or not.
🌐
cppreference.com
en.cppreference.com › cpp › string › byte › isspace
std::isspace - cppreference.com
June 28, 2024 - #include <cctype> #include <climits> #include <iomanip> #include <iostream> int main(void) { std::cout << std::hex << std::setfill('0') << std::uppercase; for (int ch{}; ch <= UCHAR_MAX; ++ch) if (std::isspace(ch)) std::cout << std::setw(2) << ch << ' '; std::cout << '\n'; }