Methods and Functions

Python String isprintable()

In Python characters that occupy the printing spaces on the display screen are considered as printable characters. These can be – letters, symbols, digits, punctuations, whitespaces, etc. There might arise a situation where the programmer might want to check if the coded program contains these printable characters or not. Because certain characters in Python strings cannot be printed to the console directly (file). In the Unicode Character Database, they are categorized as ‘Other’ or ‘Separator.’ For this purpose, the Python isprintable() function is provided. Let us learn more about this string handling method in the article below.

Definition

  • The Python isprintable() function is an in-built function that returns the value True if all the characters in the input string are printable characters; else it returns False.
  • Python isprintable() string method is used to find out if the Python input string is printable or not.

Following are the printable characters –

  • Digits ( 0123456789 )
  • Uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
  • Lowercase letters ( abcdefghijklmnopqrstuvwxyz )
  • Punctuation characters ( !”#$%&'()*+, -./:;?@[\]^_`{ | }~ )
  • Space ( )

Non-printable characters are those that are not visible and do not take up any printing space. Some Unicode characters, such as “Other” and “Separator,” are not printable. Non-Printable characters include all escape characters such as ‘\n’, \’t’, ‘\r’, ‘\x16’, ‘\xlf’, and so on.

isprintable() Syntax

The syntax followed by Python isprintable() is as follows:

                    

string.isprintable()

Python isprintable() Parameters

The Python isprintable() function does not accept any parameter.

Note – If any parameter is passed to the function, Python interpreter will raise a NameError exception.

Return Value from isprintable()

The isprintable() string method returns Boolean values. They are as follows:

  • True – if all the characters in the input string are printable characters.
  • False – if at least one of the characters in the input string is a Non-printable character.

Example 1: Working of isprintable()

Example

                    

# Python program to illustrate isprintable()
txt = 'I love Python Programming'
print(txt)
print('Is printable? -', txt.isprintable())

# empty string
txt = ' '
print(txt)
print('Is printable? -', txt.isprintable())
print('')

# symbols and numerics
txt = '^&* 123 Good morning'
print(txt)
print('Is printable? -', txt.isprintable())

# new line character
txt = '\nHello World'
print(txt)
print('Is printable? -', txt.isprintable())

Output

                    

I love Python Programming
Is printable? - True

Is printable? - True

^&* 123 Good morning
Is printable? - True

Hello World
Is printable? - False

Example 2: Non-printable characters

Example

                    

# Python program to illustrate isprintable()
txt = '\tI love Python Programming'
print('Is printable? -', txt.isprintable())

txt = 'Python 199\n'
print('Is printable? -', txt.isprintable())

# Unicode char for paragraph separator
txt = '\u2029'
print('Is printable? -', txt.isprintable())

Output

                    

Is printable? - False
Is printable? - False
Is printable? - False

Example 3: How to use isprintable()?

Example

                    

# 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 = '5+5 = 10'
if s.isprintable() == True:
    print('Printable')
else:
    print('Not Printable')

Output

                    

Not Printable
Printable

Example 4: Exceptions

Some characters (such as \u0066 -> f) can be considered as printable characters, whereas others (such as \u0009 -> \t) cannot be printed.

Example

                    

txt = 'These gifts are \u0066rom me'
print(txt)
print('Is printable? -', txt.isprintable())

txt = 'I love \u0009o play Cricket'
print(txt)
print('Is printable? -', txt.isprintable())

Output

                    

These gifts are from me
Is printable? - True

I love   o play Cricket
Is printable? - False

Frequently Asked Questions

Q1. What is printable in Python?

In Python characters that occupy the printing spaces on the display screen are considered as printable characters. These can be – letters, symbols, digits, punctuations, whitespaces, etc.

Following are the printable characters –

  • Digits ( 0123456789 )
  • Uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
  • Lowercase letters ( abcdefghijklmnopqrstuvwxyz )
  • Punctuation characters ( !”#$%&'()*+, -./:;?@[\]^_`{ | }~ )
  • Space ( )

On the other hand, Non-printable characters are those that are not visible and do not take up any printing space. Some Unicode characters, such as “Other” and “Separator,” are not printable. Non-Printable characters include all escape characters such as ‘\n’, \’t’, ‘\r’, ‘\x16’, ‘\xlf’, and so on.

Q2. How do you check if all the characters in a string are printable?

Python provides us with an isprintable() function that helps us to check if all the characters in a string are printable. The Python isprintable() function is an in-built function that returns the value True if all the characters in the input string are printable characters; else it returns False.

The syntax followed by Python isprintable() is as follows:

                    

string.isprintable()

Example

                    

txt = 'We are going to a school trip'
print('Is printable? -', txt.isprintable())

txt = 'My age is 30 and my mobile is *****9634'
print('Is printable? -', txt.isprintable())

txt = '\nHello, how are you?\t'
print('Is printable? -', txt.isprintable())

Output

                    

Is printable? - True
Is printable? - True
Is printable? - False

Q3. How do you print %d in Python?

The %d operator is used to specify integers, decimals, or numbers as a placeholder. It enables us to print numbers contained within strings or other values. Wherever the integer is to be provided, the %d operator is used. Floating-point numbers are automatically transformed to decimal values. The decimal and rational numbers are also rounded off to the absolute integral component, and the numbers after the decimal are scraped out, leaving only the integers.

Example

                    

num = 28
print('My age is %d' %num)

num = 10.9323
print('Rounded off value is %d' %num)

frac = 10/3
print('Rounded off value is %d' %frac)

Output

                    

My age is 28
Rounded off value is 10
Rounded off value is 3

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.