Python String isspace() Method

19-02-23 Ahmed Obaid 993 0

​The isspace() function checks if the characters in a string are whitespace. If the characters in the string are whitespace, it will return the boolean value True. Otherwise, it will return the boolean value False. The isspace() function space, newline, tabs, etc. is treated the same as white space and is defined in the Unicode character database as in the following examples.


  • ‘ ‘ – Space

  • ‘\t’ – Horizontal tab

  • ‘\n’ – Newline

  • ‘\v’ – Vertical tab

  • ‘\f’ – Feed

  • ‘\r’ – Carriage return

 


How to formulate it like this:


string. isspace()

Parameters: The isspace() function does not take any parameters.

return value:

True: if the characters in the string are whitespace.

False: If the string contains one or more characters that are not white spaces.

Errors and exceptions:

If a parameter is passed to the isspace() function, an error occurs


Example:


# Define a variable composed of white spaces
string = " "
#isspace function call
result = string. isspace() # returns True
# Print the result
print(result)
# Define a variable composed of a line tagundefined new
 string = "\n \n \n"
 #isspace function call
 result = string. isspace() # returns True
 # Print the result
 print(result)
 # Define a variable that contains characters and white spaces
 string = "learn"
 #isspace function call
 result = string. isspace() # returns False
 # Print the result
 print(result)

 The output will be:


 True
 True
 False

 External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python

Share page