Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the printable constant in Python?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The string module

In Python, string is a built-in module. It is a collection of different constants and classes for working with strings. This module has to be imported before use.

The printable constant

The printable constant in the string module contains the ASCII characters which are considered printable as a string.

The value of the constant is as follows:

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c

The value of the constant is a string consisting of the following constants:

  1. Digits
  2. ASCII_letters
  3. Punctuation
  4. Whitespace

The string constants above are considered printable. Hence, the printable constant consists of the above constants combined together as a string.

Syntax

string.printable

We can access the printable constant via the string module.

Example 1

import string
printable_output = string.printable
print("string.printable = '%s'" % (printable_output))
Example of the printable constant

Explanation

  • Line 1: We import the string module.

  • Line 3: We store the output of string.printable in the variable printable_output.

  • Line 5: We print the variable printable_output.

Example 2

import string
def all_chars_printable(str_input):
for i in str_input:
if i not in string.printable:
return False
return True
str_to_check_1 = "abjiaosfÇ"
print("Is %s printable? %s" % (str_to_check_1, all_chars_printable(str_to_check_1)))
str_to_check_2 = "abji232daosfdgf()#$#RFDFD"
print("Is %s printable? %s" % (str_to_check_2, all_chars_printable(str_to_check_2)))
Example of the printable constant

Explanation

  • Line 1: We import the string module.

  • Lines 3–9: We define a function called all_chars_printable that accepts a string as its parameter and checks whether this string contains only printable characters.

  • Line 11: We define a string called str_to_check_1 that contains ASCII and Unicode characters.

  • Line 12: We invoke the all_chars_printable function by passing str_to_check_1 as the parameter. The function returns False indicating that str_to_check_1 consists of non-printable characters, meaning the characters not present in the string.printable constant.

  • Line 14: We define a string called str_to_check_2 that contains only ASCII characters.

  • Line 15: We invoke the all_chars_printable function passing str_to_check_2 as the parameter. The function returns True indicating that str_to_check_2 consists of printable characters only, meaning the characters present in the string.printable constant.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring