Change language

isprintable () in Python and its applications

|

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)
  • Uppercase letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
  • 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. 

Examples:

 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 ())

Output:

 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. 
Examples:

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

Algorithm:
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)

Output:

 3 Python.Engineering name is CS portal 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically