×

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

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

Python String isalnum() method

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

1 minute read.

How to Install PIP In Python

How to Install PIP In Python The libraries for Python have made our work easier than we expected. From a simple addition of two numbers to applying algorithms on the big...

4 minutes read.

Write a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

5 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

4 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Anaconda python 3 installation for windows 10

If you are a problem solver and like to solve programming questions, the anaconda distribution for python could be one of the best options to use and solve problems in...

3 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

Decision Tree Classification in Python

In this article, we will learn the implementation of the decision tree in Sklearn, which is nothing but the Scikit Learn library of python. First, we should learn what classification...

12 minutes read.

OSError in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

4 minutes read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

PyShark in Python

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.