Python String isspace() and its application
In Python, isspace() is a built-in method used for string handling. The isspace() methods returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”.
This function is used to check if the argument contains all whitespace characters such as :
- ‘ ‘ – Space
- ‘\t’ – Horizontal tab
- ‘\n’ – Newline
- ‘\v’ – Vertical tab
- ‘\f’ – Feed
- ‘\r’ – Carriage return
Syntax :
string.isspace() Parameters: isspace() does not take any parameters Returns : 1.True- If all characters in the string are whitespace characters. 2.False- If the string contains 1 or more non-whitespace characters.
Examples:
Input : string = 'Geeksforgeeks' Output : False Input : string = '\n \n \n' Output : True Input : string = 'Geeks\nFor\nGeeks' Output : False
# Python code for implementation of isspace() # checking for whitespace characters string = 'Geeksforgeeks' print(string.isspace()) # checking if \n is a whitespace character string = '\n \n \n' print(string.isspace()) string = 'Geeks\nfor\ngeeks'print( string.isspace()) |
Output:
False True False
Application
Given a string in python, count number of whitespace characters in the string.
Example :
Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 8
Algorithm
1. Traverse the given string character by character upto its length, check if character is a whitespace character.
2. If it is a whitespace character, increment the counter by 1, else traverse to the next character.
3. Print the value of the counter.
# Python implementation to count whitespace characters in a string # Given string # Initialising the counter to 0 string = 'My name is Ayush'count=0 # Iterating the string and checking for whitespace characters # Incrementing the counter if a whitespace character is found # Finally printing the count for a in string: if (a.isspace()) == True: count+=1print(count) string = 'My name is \n\n\n\n\nAyush'count = 0for a in string: if (a.isspace()) == True: count+=1print(count) |
Output:
3 8
Recommended Posts:
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
- Python String isalpha() and its application
- Python String isdigit() and its application
- Python String isnumeric() and its application
- numpy string operations | isspace() function
- Python | Pandas Series.str.isspace() method
- divmod() in Python and its application
- isprintable() in Python and its application
- English dictionary application using Python
- Birthday Reminder Application in Python
- Python | Create simple animation for console-based application
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Merge Tuple String List values to String
- Python | Check if string ends with any string in given list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



