Check if string is printable python

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\\  ♀♂'

In this tutorial, you will learn about the Python String isprintable() method with the help of examples.

The isprintable() method returns True if all characters in the string are printable. If not, it returns False.

Example

text = 'apple'

# returns True if text is printable result = text.isprintable()

print(result) # Output: True


isprintable() Syntax

The syntax of the isprintable() method is:

string.isprintable()

Here, isprintable() checks if string is printable or not.

Note:

  • Characters that occupy printing space on the screen are known as printable characters. For example letters and symbols, digits, punctuation, whitespace
  • Characters that do not occupy a space and are used for formatting is known as non-printable characters. For example line breaks, page breaks


isprintable() Parameters

The isprintable() method doesn't take any parameters.


isprintable() Return Value

The isprintable() method returns:

  • True - if all characters in the string are printable
  • False - if the string contains at least one non-printable character

Example 1: Python String isprintable()

text1 = 'python programming'

# checks if text1 is printable result1 = text1.isprintable()

print(result1) text2 = 'python programming\n'

# checks if text2 is printable result2 = text2.isprintable()

print(result2)

Output

True
False

In the above example, we have used the isprintable() method to check whether the text1 and text2 strings are printable or not.

Here,

  • text1.isprintable() - returns True because 'python programming' is printable string
  • text2.isprintable() - returns False because 'python programming\n' contains a non-printable character '\n'

That means if we print text2, it does not print '\n' character,

print(text2)
# Output: python programing

Example 2: isprintable() with Empty String

The isprintable() method returns True when we pass an empty string. For example,

empty_string = ' '

# returns True with an empty string print(empty_string.isprintable())

Output

True

Here, empty_string.isprintable() returns True.


Example 3: isprintable() with String Containing ASCII

# defining string using ASCII value 
text = chr(27) + chr(97)

# checks if text is printable if text.isprintable():

print('Printable') else: print('Not Printable')

Output

Not Printable

In the above example, we have defined the text string using ASCII. Here,

  • chr(27) - is escape character i.e. backslash \
  • chr(97) - is letter 'a'

The text.isprintable() code returns False since chr(27) + chr(97) contains non printable escape character.

Hence the program executes the else part.


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.

What is not printable in Python?

Non-printable characters are characters that are not visible and do not occupy a space in printing. Some characters in the Unicode character database as "Other" or "Separator" are non-printable.

What is Isascii function in Python?

The isascii() method returns True if all the characters are ascii characters (a-z). Check our ASCII Reference.

How do I use Isspace in Python?

Example 2: How to use isspace()? s = '\t \n' if s. isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters') s = '2+2 = 4' if s. isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters. ')