Python String - printable()
Last Updated :
07 Jan, 2025
In Python string.printable is a pre-initialized string constant that contains all characters that are considered printable. This includes digits, ASCII letters, punctuation, and whitespace characters.
Let's understand with an example:
Python
import string
# to show the contents of string.printable
print(string.printable)
Output0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Explanation:
- string.printable: This will print all the printable characters, including digits, letters, punctuation, and whitespace characters.
Syntax of string.printable()
string.printable
Parameters:
- This doesn't take any parameters since it's a string constant, not a function.
Returns:
- It returns a string that contains all printable characters, including digits, ASCII letters, punctuation, and whitespace characters.
To identify printable characters
To identify printable characters in Python, we can use the string.printable
constant, which includes digits, letters, punctuation, and whitespace. This is useful for filtering or validating input data to ensure it contains only printable characters.
Python
# Importing string library
import string
# An input string.
Sentence = "Hey, Geeks !, How are you?"
for i in Sentence:
# Checking whether the character is a printable value
if i in string.printable:
# Printing the printable values
print("printable Value is: " + i)
Output:
printable Value is: H
printable Value is: e
printable Value is: y
printable Value is:,
printable Value is:
printable Value is: G
printable Value is: e
printable Value is: e
printable Value is: k
printable Value is: s
printable Value is: !
printable Value is:,
printable Value is:
printable Value is: H
printable Value is: o
printable Value is: w
printable Value is:
printable Value is: a
printable Value is: r
printable Value is: e
printable Value is:
printable Value is: y
printable Value is: o
printable Value is: u
printable Value is: ?
Explanation:
for i in s:
Loops over each character in `s`.-
if i in string.printable:
This checks if the character is printable. print("printable Value is: " + i):
This
displays each printable character.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice