Python String isprintable() Method with Examples

In the previous article, we have discussed Python String isidentifier() Method Examples
isprintable() Method in Python:

Python String isprintable() is a built-in string handling method. If all characters in the string are printable or the string is empty, the isprintable() method returns “True.” Otherwise, it returns “False.” This function determines whether or not the argument contains any printable characters, such as:

  • Digits or numbers ( 0123456789 )
  • Uppercase alphabets ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
  • Lowercase alphabets ( abcdefghijklmnopqrstuvwxyz )
  • Punctuation ( !”#$%&'()*+, -./:;?@[\]^_`{ | }~ )
  • whitespace ( )

Syntax:

string.isprintable()

Parameters: This function has no parameters.

Return Value:

This method returns:

  • True if the string is empty or if all of its characters are printable.
  • If the string contains at least one non-printable character, it returns False.

Examples:

Example1:

Input:

Given string = 'Welcome to Python-programs'
Given string = 'hello this is\n btechgeeks'
Given string = ' '

Output:

The given string =  Welcome to Python-programs
Checking if the given string is printable or not =  True
The given string =  hello this is
 btechgeeks
Checking if the given string is printable or not =  False
The given string =   
Checking if the given string is printable or not =  True

Example2:

Input:

Given string = '/n good morning btechgeeks\n'
Given string = 'hello this a python platform'
Given string = '_welcome all_'

Output:

The given string =  /n good morning btechgeeks

Checking if the given string is printable or not =  False
The given string =  hello this a python platform
Checking if the given string is printable or not =  True
The given string =  _welcome all_
Checking if the given string is printable or not =  True

String isprintable() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply isprintable() function to the given string in which if all characters in the given string are printable or the string is empty, the isprintable() method returns “True.” Otherwise, it returns “False.”
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string is printable or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = 'Welcome to Python-programs'
# Apply isprintable() function to the given string in which if all characters
# in the given string are printable or the string is empty,
# the isprintable() method returns "True." Otherwise, it returns "False."
# Store it in another variable.
rslt_1 = gvn_strng1.isprintable()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string is printable or not.
print("Checking if the given string is printable or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = 'hello this is\n btechgeeks'
rslt_2 = gvn_strng2.isprintable()
print("The given string = ", gvn_strng2)
print("Checking if the given string is printable or not = ", rslt_2)

gvn_strng3 = ' '
rslt_3 = gvn_strng3.isprintable()
print("The given string = ", gvn_strng3)
print("Checking if the given string is printable or not = ", rslt_3)

Output:

The given string =  Welcome to Python-programs
Checking if the given string is printable or not =  True
The given string =  hello this is
 btechgeeks
Checking if the given string is printable or not =  False
The given string =   
Checking if the given string is printable or not =  True

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.