TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python ASCII Table Generator: chr

Generate an ASCII table in a for-loop with chr and string constant literals.
ASCII table. Letters are only a representation of numbers on our computer systems. The lowercase letter "A" for example is 97. This system is called ASCII.
In a sense, the letter "A" is not "A" but is rather 97. With ASCII numbers we can test ranges of characters fast and easily. With Python we can output an ASCII table.
ASCII Table: 0 CONTROL 1 CONTROL 2 CONTROL 3 CONTROL 4 CONTROL 5 CONTROL 6 CONTROL 7 CONTROL 8 CONTROL 9 WHITESPACE 10 WHITESPACE 11 WHITESPACE 12 WHITESPACE 13 WHITESPACE 14 CONTROL 15 CONTROL 16 CONTROL 17 CONTROL 18 CONTROL 19 CONTROL 20 CONTROL 21 CONTROL 22 CONTROL 23 CONTROL 24 CONTROL 25 CONTROL 26 CONTROL 27 CONTROL 28 CONTROL 29 CONTROL 30 CONTROL 31 CONTROL 32 WHITESPACE 33 ! PUNCTUATION 34 " PUNCTUATION 35 # PUNCTUATION 36 $ PUNCTUATION 37 % PUNCTUATION 38 & PUNCTUATION 39 ' PUNCTUATION 40 ( PUNCTUATION 41 ) PUNCTUATION 42 * PUNCTUATION 43 + PUNCTUATION 44 , PUNCTUATION 45 - PUNCTUATION 46 . PUNCTUATION 47 / PUNCTUATION 48 0 DIGIT 49 1 DIGIT 50 2 DIGIT 51 3 DIGIT 52 4 DIGIT 53 5 DIGIT 54 6 DIGIT 55 7 DIGIT 56 8 DIGIT 57 9 DIGIT 58 : PUNCTUATION 59 ; PUNCTUATION 60 < PUNCTUATION 61 = PUNCTUATION 62 > PUNCTUATION 63 ? PUNCTUATION 64 @ PUNCTUATION 65 A ASCII_LETTER 66 B ASCII_LETTER 67 C ASCII_LETTER 68 D ASCII_LETTER 69 E ASCII_LETTER 70 F ASCII_LETTER 71 G ASCII_LETTER 72 H ASCII_LETTER 73 I ASCII_LETTER 74 J ASCII_LETTER 75 K ASCII_LETTER 76 L ASCII_LETTER 77 M ASCII_LETTER 78 N ASCII_LETTER 79 O ASCII_LETTER 80 P ASCII_LETTER 81 Q ASCII_LETTER 82 R ASCII_LETTER 83 S ASCII_LETTER 84 T ASCII_LETTER 85 U ASCII_LETTER 86 . ASCII_LETTER 87 . ASCII_LETTER 88 . ASCII_LETTER 89 . ASCII_LETTER 90 Z ASCII_LETTER 91 [ PUNCTUATION 92 \ PUNCTUATION 93 ] PUNCTUATION 94 ^ PUNCTUATION 95 _ PUNCTUATION 96 ` PUNCTUATION 97 a ASCII_LETTER 98 b ASCII_LETTER 99 c ASCII_LETTER 100 d ASCII_LETTER 101 e ASCII_LETTER 102 f ASCII_LETTER 103 g ASCII_LETTER 104 h ASCII_LETTER 105 i ASCII_LETTER 106 . ASCII_LETTER 107 . ASCII_LETTER 108 . ASCII_LETTER 109 . ASCII_LETTER 110 . ASCII_LETTER 111 . ASCII_LETTER 112 . ASCII_LETTER 113 . ASCII_LETTER 114 . ASCII_LETTER 115 . ASCII_LETTER 116 . ASCII_LETTER 117 . ASCII_LETTER 118 . ASCII_LETTER 119 . ASCII_LETTER 120 . ASCII_LETTER 121 . ASCII_LETTER 122 z ASCII_LETTER 123 { PUNCTUATION 124 | PUNCTUATION 125 } PUNCTUATION 126 ~ PUNCTUATION 127 CONTROL
Example program. To begin we loop over the first 128 numbers from 0 to 127 inclusive. We convert each integer to a 1-character string with chr.

Then: We test the value of the string against constant string literals in the string module.

Tip: If a string is founding ascii_letters we can print its classification as well as its value.

Note: The classification logic here is not strictly required for an ASCII table, but it helps for readability.

Python program that generates ASCII table import string for i in range(128): string_value = chr(i) # Test string value against constant strings. if string_value in string.whitespace: print(i, "WHITESPACE") elif string_value in string.ascii_letters: print(i, string_value, "ASCII_LETTER") elif string_value in string.punctuation: print(i, string_value, "PUNCTUATION") elif string_value in string.digits: print(i, string_value, "DIGIT") else: # Assume others are control characters. print(i, "CONTROL")
Notes, above program. We do not print "CONTROL" characters as they are displayed as strange characters. In HTML files it is usually best to use simple characters when possible.
Notes, strings versus ints. Suppose you have a one-character string. This requires more effort to store and handle than an int, which usually requires just 4 bytes.

And: An int can be modified with increment, decrement and other numeric operations. So a char can be quickly modified.

A summary. ASCII is an important concept in computer programs. Strings are not a low-level data type. But ints, which indicate ASCII characters, are. This is key to many optimizations.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf