Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = ' \t'
result = text.isspace()
print(result)Top-level scope
' \t'Object 2 · boolTrueisspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace.
(no output)
What do you expect this program to print?
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: TrueChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = ' \t'
result = text.isspace()
print(result)Top-level scope
' \t'Object 2 · boolTrueThe example has finished. Compare the final output with the values and references above.
True
What do you expect this program to print?
text = ' \t'
result = text.isspace()
print(result)isspace() MethodThe 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.
string.isspace()
True – if the string has only whitespace charactersFalse – if the string contains any non-whitespace character or is emptytext = " "
print(text.isspace())
True
Since the string contains only spaces, isspace() returns True.
text = "\t\n "
print(text.isspace())
True
Tabs (\t) and newlines (\n) are also considered whitespace characters.
text = " abc "
print(text.isspace())
False
Even one non-whitespace character makes the method return False.
text = ""
print(text.isspace())
False
isspace() does not return True for empty strings.
"" (empty string) returns True – it returns FalseIn text cleaning or validation tasks, isspace() is useful for quickly checking if a line or user input is just whitespace.
string.isspace() checks if a string contains only whitespace charactersFalse for empty strings or if any character is non-whitespaceWrite 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.")
Connect what you noticed in the example with the idea below.
isspace() requires a nonempty string containing only whitespace characters, including supported Unicode whitespace.
isspace() returns True for an empty string.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.