Python string Method - isprintable()

The isprintable() method is a built-in string method in Python that returns True if all characters in the string are printable characters. Printable characters are those characters that can be printed and displayed on the screen. Otherwise, it returns False.

Here is the syntax of the isprintable() method:

str.isprintable()
Source:‮www‬.theitroad.com

The isprintable() method does not take any arguments. It can be called on a string and returns a Boolean value.

Example:

# Example 1
string1 = "Hello, World!"
print(string1.isprintable()) # Output: True

# Example 2
string2 = "Hello, \nWorld!"
print(string2.isprintable()) # Output: False

# Example 3
string3 = ""
print(string3.isprintable()) # Output: True

In the above example, string1 contains only printable characters, so isprintable() method returns True. string2 contains a newline character which is not a printable character, so isprintable() method returns False. string3 is an empty string, which contains no non-printable characters, so isprintable() method returns True.