English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python String isprintable() Usage and Example

Python String Methods

If all characters in the string are printable or the string is empty, the isprintable() method will return True. Otherwise, it will return False.

Characters that occupy screen printing space are called printable characters. For example:

  • Letters and symbols

  • Numbers

  • Punctuation

  • Space

The syntax of isprintable() is:

string.isprintable()

isprintable() parameters

isprintable() method has no parameters.

isprintable() return value

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() characters

Example1: The 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())

The output when running the program is:

Space is a printable
True
New Line is printable
False
Empty string printable? True

Example2: How to use isprintable()?

# Use ASCII to write
# char(27) is an escape character
# char(97) is letter 'a'
s = chr(27) + chr(97)
if s.isprintable() == True:
  print('Printable')
else:
  print('Non-printable')
  
s = '2+2 = 4'
if s.isprintable() == True:
  print('Printable')
else:
  print('Non-printable')

The output when running the program is:

Non-printable
Printable

Python String Methods