isprintable() in Python and its application

PythonServer Side ProgrammingProgramming

In this article, we will learn about isprintable() in Python and its application.

Is printable() is a built-in method used for the purpose of string handling. The isprintable() methods return “True” when all characters present in the string are of type printable or the string is empty, Otherwise, It returns a boolean value of “False”.

Arguments − It doesn’t take any argument when called

List of printable characters include digits, letter, special symbols & spaces.

Let’s look at this illustration to check that whether the characters of string are printable or not.

Example

 Live Demo

# checking for printable characters
st= 'Tutorialspoint'
print(st.isprintable())
# checking if \n is a printable character
st= 'Tutorials \n point'
print(st.isprintable())
# checking if space is a printable character
string = ''
print( string.isprintable())

Output

True
False
True

Example

 Live Demo

# checking for printable characters
st= 'Tutorials$$point&&'
print(st.isprintable())
# checking if \t is a printable character
st= 'Tutorials \t point'
print(st.isprintable())
# checking if underscore is a printable character
string = '_'
print( string.isprintable())

Output

True
False
True

Application

  • To rectify the error of printing error at runtime we can handle this exception and replace all non-printable characters with the desired symbol to print onto the console

  • This is also useful when we have to format the output in a specific manner so as to remove the unwanted things like escape sequences

Conclusion

In this article, we learnt about the isprintable() function and its application in Python 3.x. Or earlier.

raja
Published on 28-Aug-2019 13:21:48
Advertisements