×

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 sum() function

Python sum() function The sum() function in python sums start and the items of an iterable from left to right and returns the total.  Syntax sum(iterable[, start]) Parameter iterable: This parameter represents the sequence to sum. start: It is optional...

1 minute read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

Add Element to Tuple in Python

Python: Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming...

4 minutes read.

Python String ljust() method

Python String ljust() method The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default). Syntax String.ljust(width[, fillchar]) Parameter width: This parameter represents the...

2 minutes read.

Matrix List Comprehension in Python

Introduction One of Python's most beautiful features is list comprehension. Iterating over an iterable object to create lists is a clever and succinct method. Nested List or matrix list Comprehensions, which...

6 minutes read.

Python List count() method

Python List count() method The list.count() method returns the number of times x appears in the list. Syntax list.count(x) Parameter x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.) Return This method returns the...

1 minute read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Isodate Python

Python Programming Language 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...

3 minutes read.

Working with CSV files in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

7 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.