Characters that are used for spacing are called whitespace characters. For example: tabs, spaces, newline, etc.
The syntax of isspace[]
is:
string.isspace[]
isspace[] Parameters
isspace[]
method doesn't take any parameters.
Return Value from isspace[]
isspace[]
method returns:
True
if all characters in the string are whitespace charactersFalse
if the string is empty or contains at least one non-printable character
Example 1: Working of isspace[]
s = ' \t'
print[s.isspace[]]
s = ' a '
print[s.isspace[]]
s = ''
print[s.isspace[]]
Output
True False False
Example 2: How to use 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
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Python String isspace[] method 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
Python String isspace[] Method Syntax
Syntax: string.isspace[]
Returns:
- True – If all characters in the string are whitespace characters.
- False – If the string contains 1 or more non-whitespace characters.
Python String isspace[] Method Example
Python3
string
=
"\n\t\n"
print
[string.isspace[]]
Output:
True
Example 1: Basic Intuition of isspace[] in Program
Here we will check whitespace in the string using isspace[] program.
Python3
string
=
'Geeksforgeeks'
print
[string.isspace[]]
string
=
'\n \n \n'
print
[string.isspace[]]
string
=
'Geeks\nfor\ngeeks'
print
[ string.isspace[]]
Output:
False True False
Example 2: Practical Application
Given a string in Python, count the number of whitespace characters in the string.
Input : string = 'My name is Ayush' Output : 3 Input : string = 'My name is \n\n\n\n\nAyush' Output : 8
Algorithm:
- Traverse the given string character by character up to its length, check if the character is a whitespace character.
- If it is a whitespace character, increment the counter by 1, else traverse to the next character.
- Print the value of the counter.
Python3
string
=
'My name is Ayush'
count
=
0
for
a
in
string:
if
[a.isspace[]]
=
=
True
:
count
+
=
1
print
[count]
string
=
'My name is \n\n\n\n\nAyush'
count
=
0
for
a
in
string:
if
[a.isspace[]]
=
=
True
:
count
+
=
1
print
[count]
Output:
3 8
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.
Signature
Parameters
No parameter is required.
Return
It returns either True or False.
Let's see some examples of isspace[] method to understand it's functionalities.
Python String isspace[] Method Example 1
Output:
Python String isspace[] Method Example 2
Output:
Python String isspace[] Method Example 3
isspace[] method returns true for all whitespaces like:
- ' ' - Space
- '\t' - Horizontal tab
- '\n' - Newline
- '\v' - Vertical tab
- '\f' - Feed
- '\r' - Carriage return
Output:
It contains space Not space It contains space