In this tutorial, we’ll learn what Python string isspace() method is and how to properly use it. We’ll go through multiple Python code examples to understand how isspace() method works in Python.
Outline
- Introduction: Python String isspace() Method
- Syntax of isspace() Method
- Example 1: Applying isspace() Method on a Python String
- Example 2: isspace() Method used in Python If Else Statement
- Example 3: isspace() Method Return Value
- Conclusion
Introduction: Python String isspace() Method
This method will return True if the string contains only whitespace characters like spaces, newline, tabs and will return False if the string is empty or contains non-whitespace characters.
Syntax of isspace() Method
string.isspace()
- Python string isspace() method does not take any arguments.
- It returns True if a string has only whitespace characters and will return False if the string is empty or contains non-whitespace characters.
Example 1: Applying isspace() Method on a Python String
print(' '.isspace()) print(' \t'.isspace()) print(' \n'.isspace()) print('\t \n'.isspace())
Output
True True True True
Python string isspace() method return True as the specified strings has only whitespace characters.
See below examples in which isspace() method is applied to strings containing non-whitespace characters as well.
print(' python'.isspace()) print('python3'.isspace()) print('5 \t'.isspace()) print('Python3 \n'.isspace()) print('\t r \n'.isspace()) print(''.isspace()) // empty string
Output
False False False False False False
Example 2: isspace() Method used in Python If Else Statement
val='\t ' if val.isspace(): print('String contain valid whitespace characters') else: print('String does not contain valid whitespace characters')
Output
String contain valid whitespace characters
Example 3: isspace() Method Return Value
val='\t \n\t \t ' returnedVal=val.isspace() print(returnedVal)
Output
True
Python string isspace method return True as the specified string consist of only valid whitespaces characters.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical knowledge of how to easily and properly use Python string isspace method. I’ll be looking forward to receive your valuable feedback. Thank you for reading this post.
You may like to read:
How To Use Python String IsNumeric Method – Easy Python Code Examples
How To Use Python String IsLower() Method – Easy Python Code Examples
How To Use Python String Find() Method – Easy Python Code Guide
How To Use Python String IsAlpha() Method – Easy Python Code Guide