The Python method isprintable will return True if every character within the string is printable, or if the string is empty. Conversely, it will yield False if any character in the string is classified as Nonprintable.
Syntax of Python String isprintable Method
It has the following syntax:
isprintable()
Parameters
No parameter is required.
Return
It returns either True or False.
Different Examples of Python String isprintable Method
Let's examine a few examples of the isprintable method to gain a better understanding of its features and capabilities.
Example 1
Here is a straightforward illustration that outputs True when the string can be printed. Refer to the example provided below.
# Python isprintable() method example
# Variable declaration
str = "Hello, World"
# Calling function
str2 = str.isprintable()
# Displaying result
print(str2)
Output:
Example 2
In this section, we will incorporate additional characters such as newlines and tabs within the string. Observe the outcomes from the example provided.
# Python isprintable() method example
# Variable declaration
str = "Hello, World"
str2 = "Learn Python here\n"
str3 = "\t Python is a programming language"
# Calling function
str4 = str.isprintable()
str5 = str2.isprintable()
str6 = str3.isprintable()
# Displaying result
print(str4)
print(str5)
print(str6)
Output:
True
False
False
Example 3
Evaluating various string inputs that include special characters as well. When special characters are present, the method will return False.
# Python isprintable() method example
# Variable declaration
str = "Hello, World"
if str.isprintable() == True:
print("It is printable")
else:
print("Not printable")
str = "$Hello@C# Tutorial#" # Special Chars
if str.isprintable() == True:
print("It is printable")
else:
print("Not printable")
str = "Hello\nHelloworldvisualizer"
if str.isprintable() == True:
print("It is printable")
else:
print("Not printable")
Output:
It is printable
It is printable
Not printable