isprintable() method in Python

Hello programmers. In this post, we will learn about the use of isprintable() method in Python.

In Python isprintable() is an inbuilt function for handling string. It checks whether the passed string is printable or not. If the string is printable it returns “true” otherwise “false”. It also returns “true” in case of an empty string.

The availability of inbuilt functions made Python easier and most liked programming language in comparison to others.  So let’s begin our tutorial with decent examples and explanations.

Also read: File Truncate() Method In Python

Understanding Python isprintable() method

The isprintable() method in Python checks if string passed to it contains printable characters or not. Now you must have a question that what are printable characters? Characters like a digit, uppercase- lowercase letters, special characters, and space. The only whitespace which is printable is space. Beside space all whitespaces like “\t”, “\n”, etc. are not printable in Python.

For better knowledge let’s see these examples.

def fun(str):
    res=str.isprintable()
    print(res)

str="Codespeedy Technology Pvt LTD"
fun(str)

Output:

True

All characters in “Codespeedy Technology Pvt LTD” are printable so the function returns true. What will happen if we only pass space(” “) to the function? Will, it returns “True”? See the example.

str=" "
fun(str)

Output:

True

It proves that space is a printable character. Let’s see with other whitespaces.

# \n betwwen two words
str="Codespeed \n Technology PVT LTD"
fun(str)

str="\t "
fun(str)

str="\b"
fun(str)

Output:

False
False
False

All three whitespace characters(“\n”, “\t”, “\b”) are non-printable characters that’s why function return the “Fasle”.

 

That’s enough for this tutorial. I hope you understood it well. If you want to give any suggestions related to this post please comment below. For a tutorial on other Python topics comment below the topic name.

Thank You.

Also read: Call an external command in Python

Leave a Reply