isprintable() returns True when every character can be displayed as a visible symbol or an ordinary space. Letters, digits, punctuation, and the plain space are printable.
Control characters are not printable. That includes the newline \n, the tab \t, and invisible codes such as \x00. An empty string returns True, because it holds no unprintable character.
text.isprintable() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax
Call it on any string. There are no arguments, but the parentheses are required.
python
ok = text.isprintable()
Call
What it does
"Hello, World!".isprintable()
Returns True. Letters, a comma, a space, and a symbol are all printable.
"line1\nline2".isprintable()
Returns False. The newline is a control character.
"".isprintable()
Returns True. An empty string contains nothing unprintable.
To find blank text use isspace(). The built-in repr() shows unprintable characters as escapes such as \n.
Library declarations
python
str.isprintable() -> bool
# returns: True if all characters are printable (or the string is empty)
# the space is printable; newline and tab are not
Parameters
This method takes no arguments. It always checks the whole string.
Return Value
Return Value
Description
bool
True if the string is empty or every character is printable, otherwise False.
Python Examples
Example 1 — Which Strings Are Printable?
Check a normal sentence, a text with a tab, a text with a newline, and an empty string.
python
for text in ["a b", "a\tb", "a\nb", ""]:
print(repr(text), text.isprintable())
Output
'a b' True
'a\tb' False
'a\nb' False
'' True
How It Works
The space is fine."a b" contains a normal space, which is printable.
A tab and a newline are not. Both are control characters, so the results are False.
Empty is printable. There is no unprintable character in an empty string, so it returns True.
repr() shows the escapes. Printing the repr writes the tab as \t and the newline as \n, so you can see them.
Example 2 — Clean a Log Line
Keep only the printable characters of a raw line.
python
raw = "id=7\tname=Ada\n"
clean = "".join(ch for ch in raw if ch.isprintable())
print(raw.isprintable())
print(clean)
print(clean.isprintable())
Output
False
id=7name=Ada
True
How It Works
The raw line fails. It ends with a newline and has a tab inside, so isprintable() is False.
Filter by character. The generator keeps each character whose isprintable() is True, and join() builds the new string.
The clean line passes. The tab and the newline are gone, and the new text is printable. The tab is removed, not replaced by a space.
Example 3 — Locate Unprintable Characters
Report where the control characters are in a string.
python
text = "A\x00B\x07C"
bad = [i for i, ch in enumerate(text) if not ch.isprintable()]
print(len(text))
print(bad)
Output
5
[1, 3]
How It Works
Hidden characters. The escapes \x00 and \x07 are control characters that take no visible space.
enumerate() gives positions. The comprehension keeps the index i whenever the character is not printable.
Five characters, two bad. The string has 5 characters, and the control characters sit at index 1 and index 3.
Frequently Asked Questions
It returns True if every character in the string is printable, such as letters, digits, punctuation, and the space, and False if there is a control character.
Yes. The ordinary space is printable. Tabs, newlines, and other whitespace control characters are not.
True, because there is no unprintable character in it.
Keep only the printable ones: "".join(ch for ch in text if ch.isprintable()).
isspace() checks that the string is made only of whitespace. isprintable() checks that no character is a control character, and it accepts letters and spaces together.