Check a String is Printable in Python

  • Post category:String

In this article, you will see how to check a string is printable in Python. A printable string is a string that contains only printable characters. The string isprintable() method returns True if all characters in the string are printable, otherwise False.

Step 1: Create a string

str = "\n"
print("str = ", str)
print("str type = ", type(str))

Step 2: Check if a string is printable

if all(c.isprintable() for c in str):
    print("Printable")
else:
    print("Not Printable")

Output:

str =  

str type =  <class 'str'>
Not Printable

Free resources to learn advanced skills: AiHints and CodeAllow