Python Reference Library →

Python String isspace() Method – Check for Whitespace Only

Let’s look at string isspace() method – check for whitespace only

  1. Notice

    isspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = ' \t'
result = text.isspace()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str' \t'Object 2 · boolTrue
This step

isspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace.

Picture string isspace() method – check for whitespace only

isspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace.

Image brief

Alt text
A diagram for Python String isspace() Method – Check for Whitespace Only. Instructions: text = ' \t' result = text.isspace() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: ' \t'; object 2: True
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String isspace() Method – Check for Whitespace Only". Teaching focus: isspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = ' \t' result = text.isspace() print(result) | v DISPLAYED RESULT True FINAL BINDINGS text --> object 1 (str) result --> object 2 (bool) object 1: ' \t' object 2: True Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String isspace() Method – Check for Whitespace Only. Instructions: text = ' \t' result = text.isspace() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: ' \t'; object 2: True

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = ' \t'
result = text.isspace()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str' \t'Object 2 · boolTrue
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True

What do you expect this program to print?

What does this example display?

text = ' \t'
result = text.isspace()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True

Follow the printed values in order. The output is: True

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

isspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace.

isspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace.

More examples and reference

Python String isspace() Method

The isspace() method in Python checks whether all characters in a string are whitespace characters like space, tab, newline, etc. It returns True if the string contains only whitespace and is not empty.

Syntax

string.isspace()

Returns:

  • True – if the string has only whitespace characters
  • False – if the string contains any non-whitespace character or is empty

Example 1: Basic Usage

text = "   "
print(text.isspace())
True

Since the string contains only spaces, isspace() returns True.

Example 2: Including Tabs and Newlines

text = "\t\n "
print(text.isspace())
True

Tabs (\t) and newlines (\n) are also considered whitespace characters.

Example 3: String with Non-Whitespace

text = " abc "
print(text.isspace())
False

Even one non-whitespace character makes the method return False.

Example 4: Empty String

text = ""
print(text.isspace())
False

isspace() does not return True for empty strings.

Use Cases

  • To validate input that should be empty or whitespace only
  • To filter out blank lines when processing files
  • To check for formatting issues in text

Common Mistakes

  • Assuming "" (empty string) returns True – it returns False
  • Using it on non-string objects – you must call it on a string

Interview Tip

In text cleaning or validation tasks, isspace() is useful for quickly checking if a line or user input is just whitespace.

Summary

  • string.isspace() checks if a string contains only whitespace characters
  • Returns False for empty strings or if any character is non-whitespace
  • Whitespace includes spaces, tabs, newlines, etc.

Practice Problem

Write a program that asks the user to enter something and checks if they entered only spaces:

user_input = input("Enter something: ")
if user_input.isspace():
    print("Only whitespace entered.")
else:
    print("Contains non-whitespace characters.")

What to remember

Connect what you noticed in the example with the idea below.

Remember

isspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace.

Watch out for this idea

isspace() returns True for an empty string.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →