Python String isprintable() Function
The String isprintable()
method returns True
if the string is empty or if all characters in the string are printable. Otherwise, it returns False
.
note
Generally:
- Characters that occupy printing space on the screen are known as printable characters.
- Characters that do not occupy a space and are used for formatting is known as non-printable characters.
For example:
- Carriage return
\r
, line feed\n
and tab\t
are examples of non-printable characters. - A space character
' '
(0x20, ASCII space) is considered printable.
Syntax
my_string.isprintable()
isprintable() Parameters
Python String isprintable()
function does not take any parameters.
isprintable() Return Value
Python String isprintable()
function returns:
True
if all characters in the string are printable or if the string is empty.False
if the string contains at least one non-printable character.
Examples
Example 1: Check if a String is printable with isprintable()
The isprintable()
method returns True
if all characters in the string are printable characters.
my_str = 'Tutorial Reference'
result = my_str.isprintable()
print(result) # Output: True
output
True
The isnumeric()
method returns False
if at least one character is not printable.
For example, string with line feed \n
and tab \t
is not printable:
my_str = '\tTutorial\nReference'
result = my_str.isprintable()
print(result) # Output: False
output
False
Example 2: Check if an Empty String is printable with isprintable()
An empty string is considered a printable string.
my_str = ''
result = my_str.isprintable()
print(result) # Output: True
output
True