isprintable() in Python and its application
In Python, isprintable() is a built-in method used for string handling.
The isprintable() methods 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 : 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.
Examples:
Input : string = 'My name is Ayush' Output : True Input : string = 'My name is \n Ayush' Output : False Input : string = '' Output : True
# Python code for implementation of isprintable() # checking for printable characters string = 'My name is Ayush'print(string.isprintable()) # checking if \n is a printable character string = 'My name is \n Ayush'print(string.isprintable()) # checking if space is a printable character string = '' print( string.isprintable()) |
Output:
True False True
Errors Or Exceptions
1. The function does not take any arguments, therefore no parameters should be passed, otherwise it returns an error.
2. The only whitespace character which is printable is space or ” “, otherwise every whitespace character is non-printable and the function returns “False”.
3. Empty string is considered as printable and it returns “True”.
Application : Given a string in python, count number of non-printable characters in the string and replace non-printable characters by a space.
Examples:
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:
1. Initialise an empty new string and a variable count = 0.
2. Traverse the given string character by character upto its length, check if character is a non-printable character.
3. If it is a non-printable character, increment the counter by 1, and add a space to the new string.
4. Else if it is a printable character, add it to the new string as it is
5. Print the value of the counter and the new string.
# Python implementation to count # non-printable characters in a string # Given string and new string string ='GeeksforGeeks\nname\nis\nCS portal'newstring = '' # Initialising the counter to 0 count = 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+= a print(count) print(newstring) |
Output:
3 GeeksforGeeks name is CS portal
Recommended Posts:
- divmod() in Python and its application
- Python String isalpha() and its application
- Python String isspace() and its application
- Birthday Reminder Application in Python
- Python String isdigit() and its application
- Python String isnumeric() and its application
- Python | Create simple animation for console-based application
- Python3 for GUI application | An Overview
- Flask - (Creating first simple application)
- Computer Vision module application for finding a target in a live camera
- Python | Convert list to Python array
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



