Python check if string is readable

This Python 3 string contains all kinds of special characters:

s = 'abcd\x65\x66 äüöë\xf1 \u00a0\u00a1\u00a2 漢字 \a\b\r\t\n\v\\ \231\x9a \u2640\u2642\uffff'

If you try to show it in the console (or use repr), it makes a pretty good job of escaping all non-printable characters from that string:

>>> s
'abcdef äüöëñ \xa0¡¢ 漢字 \x07\x08\r\t\n\x0b\\ \x99\x9a ♀♂\uffff'

It is smart enough to recognise e.g. horizontal tab (\t) as printable, but vertical tab (\v) as not printable (shows up as \x0b rather than \v).

Every other non printable character also shows up as either \xNN or \uNNNN in the repr. Therefore, we can use that as the test:

def is_printable(s):
    return not any(repr(ch).startswith("'\\x") or repr(ch).startswith("'\\u") for ch in s)

There may be some borderline characters, for example non-breaking white space (\xa0) is treated as non-printable here. Maybe it shouldn't be, but those special ones could then be hard-coded.


P.S.

You could do this to extract only printable characters from a string:

>>> ''.join(ch for ch in s if is_printable(ch))
'abcdef äüöëñ ¡¢ 漢字 \r\t\n\\  ♀♂'

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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:

    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. The empty string is considered printable and it returns “True”.

    Example 1

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

    Python3

    string = 'My name is Ayush'

    print(string.isprintable())

    string = 'My name is \n Ayush'

    print(string.isprintable())

    string = ''

    print( string.isprintable())

    Output: 

    True
    False
    True

    Example 2: Practical Application

    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: 

    1. Initialize an empty new string and a variable count = 0. 
    2. Traverse the given string character by character up to its length, check if the 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.

    Python3

    string ='GeeksforGeeks\nname\nis\nCS portal'

    newstring = ''

    count = 0

    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

    Is printable () in Python?

    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 )

    How do you check whether all the characters in a string is printable?

    The isprintable() method returns True if all the characters are printable, otherwise False. Example of none printable character can be carriage return and line feed.

    How do I check if a string is ASCII?

    isascii() to check if a string is in ASCII. Use str. isascii() on a string, str , to return True if the string contains only ASCII characters and False otherwise.

    How do I check if a string contains only ASCII characters?

    Return value The isascii() function returns a boolean value where True indicates that the string contains all ASCII characters and False indicates that the string contains some non-ASCII characters.