Remember
isprintable() tests whether every character is printable, or the string is empty. Newline and tab are not printable characters.
isprintable() tests whether every character is printable, or the string is empty. Newline and tab are not printable characters.
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 = 'tea'
result = text.isprintable()
print(result)Top-level scope
'tea'Object 2 · boolTrueisprintable() tests whether every character is printable, or the string is empty. Newline and tab are not printable characters.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea'
result = text.isprintable()
print(result)
|
v
DISPLAYED RESULT
True
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (bool)
object 1: 'tea'
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 = 'tea'
result = text.isprintable()
print(result)Top-level scope
'tea'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 = 'tea'
result = text.isprintable()
print(result)isprintable() MethodThe isprintable() method in Python is used to check whether all characters in a string are printable. This is helpful when validating user input, reading files, or filtering special characters.
string.isprintable()
True – if all characters are printable (including space)False – if at least one character is not printable (like \n, \t, etc.)text = "Hello123!"
print(text.isprintable())
True
text = "Hello\nWorld"
print(text.isprintable())
False
\n (newline character) is not printable, so the result is False.
text = "Python 3.10 @#%"
print(text.isprintable())
True
Spaces and symbols like @, #, % are considered printable characters.
Suppose you're processing user data or reading input from a file. You might want to ensure that it doesn't contain control characters like tabs or newlines.
user_input = "Hello\tWorld"
if not user_input.isprintable():
print("Input contains non-printable characters.")
"".isprintable() returns True. An empty string has no characters, and thus no non-printable ones.isprintable() checks for only alphabets – it includes all printable characters, even symbols.isprintable() can be useful in data validation questions. If asked to validate clean input or sanitize data, think about it!
isprintable() checks if all characters in a string are printable\n) or tab (\t) are not printableWrite a program that asks the user to enter a string and tells whether it’s fully printable or not.
text = input("Enter a string: ")
if text.isprintable():
print("All characters are printable!")
else:
print("String contains non-printable characters.")
Connect what you noticed in the example with the idea below.
isprintable() tests whether every character is printable, or the string is empty. Newline and tab are not printable characters.
A newline is printable according to isprintable().
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.