my_str.isprintable()
Returns True if all chars in a string are printable , otherwise rturns False
my_str='Welcome to plus2net'
print(my_str.isprintable()) # Output is False
my_str='Welcome \t to plus2net'
print(my_str.isprintable()) # Output is False ( presence of Tab \t)
my_str='Welcome to plus2net \n'
print(my_str.isprintable()) # Output is False ( presence of line break \n)
my_str='1234'
print(my_str.isprintable()) # Output is True
Printable characters include Unicode characters, but control characters like tabs are non-printable:
my_str = 'Hello World 你好'
print(my_str.isprintable()) # True
Check if user input contains only printable characters before processing:
user_input = 'ValidInput123!'
if user_input.isprintable():
print("Input is valid.")
Output
Input is valid.
Empty strings are considered printable:
empty_str = ''
print(empty_str.isprintable())
True
Ensure text for a report contains only printable characters:
report_text = "Final Report: All data valid."
if report_text.isprintable():
print("Ready for output.")
Ready for output.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.