Python String isprintable() Method

The isprintable() method returns True if all the characters of the given string are Printable. It returns False even if one character is Non-Printable.

The uppercase and lowercase alphabets, numerical values, symbols, and empty string all come under printable characters.

Non-printable characters are characters that are not visible and do not occupy a space in printing. Some characters in the Unicode character database as "Other" or "Separator" are non-printable. All escape characters such as '\n', '\t', '\r', '\x16', '\xlf', etc. come under Non-Printable characters.

Syntax:

str.isprintable()

Parameters:

None

Return Value:

Returns True if a string is printable; otherwise False.

All the letters, symbols, punctuations, digits are considered as printable characters, so the isprintable() returns True, as shown below.

Example: isprintable() with Printable Chars
mystr = '12345'
print(mystr.isprintable()) # Returns True

mystr = 'TutorialTeachers'
print(mystr.isprintable()) # Returns True

mystr = '#1 Harbour Side'
print(mystr.isprintable()) # Returns True

mystr = ''
print(mystr.isprintable()) # Returns True

mystr = ' '
print(mystr.isprintable()) # Returns True
Output
True
True
True
True
True

All escape characters are considered as Non-Printable characters. Consider the following example.

Example: isprintable() with Non-printable Chars
mystr = 'Hello World'
print(mystr.isprintable()) # Returns True

mystr = 'Hello\tWorld'
print(mystr.isprintable()) # Returns False

mystr = 'Hello World\n'
print(mystr.isprintable()) # Returns False

mystr = '''Hello 
 World'''
print(mystr.isprintable()) # Returns False

mystr = '\u2029' # Unicode char for paragraph separator
print(mystr.isprintable()) # Returns False
Output
True
False
False
False
False

In the above example, the isprintable() method return False if a string contains non-printable character such as '\n', '\t', etc.