Python String isprintable()

The Python String isprintable() method is a built-in function that returns true if all the characters in a string are printable or if the string is empty. If not, it returns False.

In this article, we will learn about the Python String isprintable() method with the help of examples.

What are printable characters in Python?

In Python, the character that occupies printing space on the screen is known as printable characters.

Examples of Printable characters are as follows –

  • Alphabets
  • Digits
  • whitespace
  • symbols and punctuation

isprintable() Syntax

The Syntax of isprintable() method is:


isprintable() Parameters

The isprintable() method does not take any parameters.

isprintable() Return Value

The isprintable() method returns

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

Example 1: Working of isprintable() method

# valid text and printable
text = 'These are printable characters'

# \n is not printable
text = '\nNew Line is printable'

# \t is non printable
text = '\t tab is printable'

# empty character is printable
text = ''
print('\nEmpty string printable?', text.isprintable())


These are printable characters

New Line is printable       
         tab is printable   

Empty string printable? True

Example 2: How to use isprintable() in real world?

In the below Program, we will iterate the given text to check and count how many non-printable characters are there. If we find any non-printable characters we will be replacing it with empty characters and finally we will be output the printable characters into ‘newtext’ variable which can print all the characters.

text = 'Welcome to ItsMyCode,\nPython\tProgramming\n'
newtext = ''

# Initialising the counter to 0
count = 0

# Iterate the text, count the non-printable characters
# replace non printable characters with empty string
for c in text:
    if (c.isprintable()) == False:
        count += 1
        newtext += ' '
        newtext += c
print("Total Non Printable characters count is:", count)


Total Non Printable characters count is: 3
Welcome to ItsMyCode, Python Programming 
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python List Insert()

Python List insert()

Table of Contents Hide Syntax of List insert() insert() ParametersReturn Value from List insert()Example 1: Inserting an Element to the ListExample 2: Inserting an element at the start and end of…
View Post
Menu Driven Program In Python

Menu Driven Program in Python

Introduction to Menu-Driven Program A menu driven program in Python is a program that takes input from a user by displaying a list of options and allows users to choose…
View Post
How To Copy A File In Python

How to Copy a File in Python?

Table of Contents Hide Modules to copy a file in Python shutil module top copy a file in Pythoncopy() copy2() copyfile() copyfileobj()os module to copy a file in Pythonpopen() system() subprocess…
View Post
Python Write Text File

Python Write Text File

Table of Contents Hide Steps on How to write to a File in Python Python open() function Methods for Writing to a text file in PythonPython close() functionExamples for Writing to…
View Post
Python String Rsplit()

Python String rsplit()

Table of Contents Hide rsplit() Syntaxrsplit() Parametersrsplit() Return ValueExample 1: How rsplit() works in Python?Example 2: How split() works when maxsplit is specified? The Python String rsplit() method is a…
View Post
Python Check If String Is Empty Or Not

Check if string is empty or not in Python

Table of Contents Hide Python program to check if string is empty or notUsing len() funtionUsing not operatorUsing not+str.strip() method Using not + str.isspace method In this article, you will learn…
View Post