Python String isprintable()

Python String isprintable() is used to check if given string contains only printable characters.

isprintable() method returns True if all the characters in the given string are printable, otherwise the method returns False.

In this tutorial, we will learn the syntax and examples for isprintable() method of String class.

Syntax

The syntax to call isprintable() method on string x in Python is

</>
Copy
x.isprintable()

Examples

In the following program, we take a string 'apple 314' in x, and check if this string x is printable using isprintable() method.

Example.py

</>
Copy
x = 'apple 314'
result = x.isprintable()
print(result)

Output

True

In the following program, we take a string 'apple\r314' in x, and check if this string x is printable using isprintable() method. Since, there is a non-printable character \r (return feed) in the string, the method returns False.

Example.py

</>
Copy
x = 'apple\r314'
result = x.isprintable()
print(result)

Output

False

Conclusion

In this Python Tutorial, we learned how to check if given string contains only printable characters, using String method – isprintable().