Home
Map
ascii ExampleUse ascii, ascii_lowercase and ascii_uppercase. It covers whitespace and digits.
Python
This page was last reviewed on Sep 25, 2022.
ASCII. Most common English letters are ASCII—the lowercase "a," the uppercase Z, the digit 0. With ascii we change text encoding.
Punctuation. Another important category of ASCII characters is punctuation characters like commas and quotes. We can also use string.whitespace.
First example. For many English programs, the ASCII character set is sufficient. No letters with accents occur. But sometimes Unicode is needed.
Info With ascii, a built-in, we escape Unicode characters into ASCII characters.
# This string contains an umlaut. value = "Düsseldorf" print(value) # Display letter with escaped umlaut. print(ascii(value))
Düsseldorf 'D\xfcsseldorf'
Ascii_letters, lowercase, uppercase. The string module contains helpful constants. We can use these to loop over all lowercase or uppercase ASCII letters, or to build translation tables.
Tip We import the string module and access these constants on it. This is an easy to way to access the constants.
import string # The letters constant is equal to lowercase + uppercase. print(string.ascii_letters) print(string.ascii_lowercase) print(string.ascii_uppercase)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
String.digits. This is another constant in the string module. If you ever need to loop over the strings "0" through "9" this is an option. The code is clean and simple.
import string # Loop over digits using string.digits constant. for digit in string.digits: print(digit)
0 1 2 3 4 5 6 7 8 9
A summary. We can access constant strings like string.ascii_letters to get ascii values. To change text encoding we can use the ascii built-in.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 25, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.