isprintable () in Python and its applications

Counters | Python Methods and Functions

In Python, isprintable () — it is a built-in method used to process strings. 
The isprintable () methods return True if all characters in the string are printable or if the string is empty. Otherwise, "False" is returned. 
This function is used to check if the argument contains any printable characters, such as:

  • Numbers (0123456789)
  • Lowercase letters (abcdefghijklmnopqrstuvwxyz)
  • Punctuation (! "# $% & amp; & # 39; () * +, -./:;[email protected] [/ ]^_' { |} ~)
  • Space ()

Syntax :

  string.isprintable ()   Parameters:  isprintable () does not take any parameters  Returns:  1.True- If all characters in the string are printable or the string is empty. 2.False- If the string contains 1 or more non printable characters. 


 Input: string = 'My name is Ayush' Output: True Input: string =' My name is Ayush' Output: False Input: string = '' Output: True 

# Python code to implement isprintable ()

# check for printable characters

string = ' My name is Ayush'

print (string.isprintable ())

# check is or / n a printable character

string = 'My name is Ayush'

print (string. isprintable ())

# check if a space is a printable character

string = ''

print (string.isprintable ())


 True False True 

Errors or exceptions
1. The function takes no arguments, so no parameters must be passed, otherwise it returns an error. 
2. The only space that can be printed is space or "", otherwise every space character cannot be printed and the function returns "False". 
3. An empty string is considered printable and returns "True".

Usage: For a given string in python, count the number of non-printable characters in the string and replace the non-printable characters with a space. 

 Input: string = 'My name is Ayush' Output: 0 My name is Ayush Input: string =' My name is Ayush' Output: 3 My name is Ayush 

1. Initialize an empty newline and the variable count = 0.
2. Go through the given string character by character to its length, check if the character is unprintable character. 
3. If it is a non-printable character, increment the counter by 1 and add a space on the new line. 
4. Otherwise, if it is a printable character, add it on a new line as it is. 
5. Print the counter value and a new line.

# Python implementation for counting
# non-printable characters per line

# Specified line and new line

string = 'Python.Engineering name is CS portal '

newstring = ' '

# Initialize counter to 0

count = 0

# Iterating a string and
# pr overriding non-printable characters
# Increasing the counter if
# unprintable character found
# and replace it with a space on a new line

# Finally, print quantity and newline


for a in string:

if (a.isprintable ()) = = False :

count + = 1

newstring + = ''

else :

newstring + = a

print (count)

print (newstring)


 3 Python.Engineering name is CS portal