The isprintable() function in python helps to check whether all the characters in the string are printable characters or an empty string. The function returns True if all the characters are printable or it is an empty string, otherwise it will return False.
string.isprintable()
The isprintable() method doesn't take any parameters. The printable characters in the string include letters and symbols, digits, punctuation, whitespace. Non-printable characters are those which are not visible and do not occupy a space in printing(escape characters such as '\n', '\t', '\r', '\x16', '\xlf', etc).
The return value is always a boolean value. The function returns true even for an empty string because it is considered as printable.
Input | Return Value |
---|---|
all characters are printable | True |
at least one non-printable | False |
string = 'Space is a printable?'
print(string)
print(string.isprintable())
string = '\nNew Line is printable?'
print(string)
print(string.isprintable())
string = ''
print('\nEmpty string is printable?', string.isprintable())
Output:
Space is a printable? True New Line is printable? False Empty string is printable? True
# chr(27) is escape character,char(97) is letter 'a'
string = chr(27) + chr(97)
if string.isprintable() == True:
print('Is Printable')
else:
print('Not Printable')
string = '5+5 = 10'
if string.isprintable() == True:
print('Is Printable')
else:
print('Not Printable')
Output:
Not Printable Is Printable