Python isprintable() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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()
CallWhat 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 ValueDescription
boolTrue 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())

How It Works

  1. The space is fine. "a b" contains a normal space, which is printable.
  2. A tab and a newline are not. Both are control characters, so the results are False.
  3. Empty is printable. There is no unprintable character in an empty string, so it returns True.
  4. 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())

How It Works

  1. The raw line fails. It ends with a newline and has a tab inside, so isprintable() is False.
  2. Filter by character. The generator keeps each character whose isprintable() is True, and join() builds the new string.
  3. 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)

How It Works

  1. Hidden characters. The escapes \x00 and \x07 are control characters that take no visible space.
  2. enumerate() gives positions. The comprehension keeps the index i whenever the character is not printable.
  3. 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.

Next: isspace()

True if the string is not empty and holds only whitespace characters.

isspace() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful