Python String isprintable()

The isprintable() methods returns True if all characters in the string are printable or the string is empty. If not, it returns False.

Characters that occupies printing space on the screen are known as printable characters. For example:

  • letters and symbols
  • digits
  • punctuation
  • whitespace

The syntax of isprintable() is:

string.isprintable()

isprintable() Parameters

The isprintable() method doesn't take any parameters.


Return Value from isprintable()

The isprintable() method returns:

  • True if the string is empty or all characters in the string are printable
  • False if the string contains at least one non-printable() character

Example 1: Working of isprintable()

s = 'Space is a printable'
print(s)
print(s.isprintable())

s = '\nNew Line is printable'
print(s)
print(s.isprintable())

s = ''
print('\nEmpty string printable?', s.isprintable())

When you run the program, the output will be:

Space is a printable
True

New Line is printable
False

Empty string printable? True

Example 2: How to use isprintable()?

# written using ASCII
# chr(27) is escape character
# char(97) is letter 'a'
s = chr(27) + chr(97)

if s.isprintable() == True:
  print('Printable')
else:
  print('Not Printable')
  
s = '2+2 = 4'

if s.isprintable() == True:
  print('Printable')
else:
  print('Not Printable')

When you run the program, the output will be:

Not Printable
Printable