Python String isprintable() Method - GeeksforGeeks

12 Aug, 2021
Python String isprintable() is a built-in method used for string handling. The isprintable() method returns “True” if all characters in the string are printable or the string is empty, Otherwise, It returns “False”. This function is used to check if the argument contains any printable characters such as:
Digits ( 0123456789 )
Uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
Lowercase letters ( abcdefghijklmnopqrstuvwxyz )
Punctuation characters ( !”#$%&'()*+, -./:;?@[\]^_`{ | }~ )
Space ( )
Syntax: 
string.isprintable()
Parameters:
isprintable() does not take any parameters
Returns:
True – If all characters in the string are printable or the string is empty.
False – If the string contains 1 or more nonprintable characters.
Errors Or Exceptions:
The function does not take any arguments, therefore no parameters should be passed, otherwise, it returns an error.The only whitespace character which is printable is space or ” “, otherwise every whitespace character is non-printable and the function returns “False”.The empty string is considered printable and it returns “True”.
The function does not take any arguments, therefore no parameters should be passed, otherwise, it returns an error.
The only whitespace character which is printable is space or ” “, otherwise every whitespace character is non-printable and the function returns “False”.
The empty string is considered printable and it returns “True”.
Input : string = 'My name is Ayush'
Output : True

Input : string = 'My name is \n Ayush'
Output : False

Input : string = ''
Output : True
Python3
# Python code for implementation of isprintable()  # checking for printable charactersstring = 'My name is Ayush'print(string.isprintable())  # checking if \n is a printable characterstring = 'My name is \n Ayush'print(string.isprintable())  # checking if space is a printable characterstring = ''print( string.isprintable())
Output: 
True
False
True
Given a string in python, count the number of non-printable characters in the string and replace non-printable characters with a space. 
Input : string = 'My name is Ayush'
Output : 0
         My name is Ayush

Input : string = 'My\nname\nis\nAyush'
Output : 3
         My name is Ayush
Algorithm: 
Initialize an empty new string and a variable count = 0. Traverse the given string character by character up to its length, check if the character is a non-printable character. If it is a non-printable character, increment the counter by 1, and add a space to the new string. Else if it is a printable character, add it to the new string as it is.Print the value of the counter and the new string.
Initialize an empty new string and a variable count = 0. 
Traverse the given string character by character up to its length, check if the character is a non-printable character. 
If it is a non-printable character, increment the counter by 1, and add a space to the new string. 
Else if it is a printable character, add it to the new string as it is.
Print the value of the counter and the new string.
Python3
# Python implementation to count # non-printable characters in a string  # Given string and new stringstring ='GeeksforGeeks\nname\nis\nCS portal'newstring = ''  # Initialising the counter to 0count = 0  # Iterating the string and # checking for non-printable characters# Incrementing the counter if a # non-printable character is found # and replacing it by space in the newstring  # Finally printing the count and newstring  for a in string:    if (a.isprintable()) == False:            count+= 1            newstring+=' '    else:            newstring+= aprint(count)print(newstring)
Output: 
3
GeeksforGeeks name is CS portal
AmiyaRanjanRout
Python-Built-in-functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Read a file line by line in Python
Taking input in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe