×

Python String isspace() method

Python String isspace() method

The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given string contains any whitespace characters. The common example of whitespace characters are as follows:

  • ‘ ‘ – Space
  • ‘\t’ – Horizontal tab
  • ‘\n’ – Newline
  • ‘\v’ – Vertical tab
  • ‘\f’ – Feed
  • ‘\r’ – Carriage return

Syntax

String.isspace()

Parameter

NA

Return

This method returns a boolean value “True” if all characters in the string are whitespace characters, else it returns “False” if the string contains 1 or more non-whitespace characters.

Example 1

 # Python program explaining
 # the isspace() method
 strVal = "   \t"
 print("String 1=",strVal)
 print("The isspace() method returns: ",strVal.isspace())
 strVal = " a "
 print("String 2=",strVal)
 print("The isspace() method returns: ",strVal.isspace())
 strVal = ""
 print("String 3=",strVal)
 print("The isspace() method returns: ",strVal.isspace()) 

Output

 String 1=     
 The isspace() method returns:  True
 String 2=  a 
 The isspace() method returns:  False
 String 3= 
 The isspace() method returns:  False 

Example 2

 # Python program explaining
 # the isspace() method
 # initializing a string value
 string = "Tutorials and Examples"
 print(string.isspace()) 
 # checking if \n is a whitespace character 
 string = "\t \t \t"
 print(string.isspace()) 
 # if the string contains 1 or more non-whitespace characters
 # this method returns false
 string = "Tutorials\nand\nExamples"
 print( string.isspace()) 

Output

 False
 True
 False 

Example 3

 # Python program explaining
 # the isspace() method
 # initializing a string value
 string = '\t  \n'
 print("String 1:",string)
 if string.isspace() == True:
   print('All the characters in the string are whitespace characters')
 else:
   print('The string contain 1 or more non-whitespace characters')
 # initializing a second string 
 string = '7*2 = 14'
 print("String 2:",string)
 if string.isspace() == True:
   print('All the characters in the string are whitespace characters')
 else:
   print('The string contain 1 or more non-whitespace characters') 

Output

 String 1:    
 All the characters in the string are whitespace characters
 String 2: 7*2 = 14
 The string contain 1 or more non-whitespace characters 

Related Topics

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

Python Selectors

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Python String Variable

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

4 minutes read.

Screen Rotation app Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Python Memory Management

In order to manage memory, Python uses a private heap that contains all of its data structures and objects. The Python memory manager is responsible for the internal management of...

11 minutes read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Base Case in Recursive function python

In this article, we will learn about Python's base case in a recursive function. Before learning this, let’s first understand what recursion is in Python and the use of recursion in...

6 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

After Python, What Should I Learn

Python is a programming language used to create websites, software, and other projects. It is easy to code and easy to learn. After learning Python, there are many different directions...

4 minutes read.

Falcon Python

Introduction of Python Python is the fastest and the smartest programming language and it is an object oriented language. Python has libraries that can be importedeasily and perform many operations; to...

3 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.