Represent integers as characters and characters as integers.

language-essentials

Description

chr() and ord() are methods that allow you to represent integers as characters and characters as integers respectively in python. These functions rely on ASCII numbers to do the conversions.

Definitions

chr()

A built in function that takes an int (representing an ordinance/ASCII number) and returns a string of the corresponding single character. The inverse of the ord().function.

chr(65) # Returns 'A'
chr(97) # Returns 'a'

ord()

A built in function that takes a single character string and returns an int (representing its ordinance/ASCII number). The inverse of chr()

ord("A") # Returns 65
ord("a") # Returns 97

ASCII (American Standard Code for Information Interchange)

A look up table to standardize the representation of characters. For example the character 'a' is always 97 in ASCII and vice-versa.

Usage

In this repo you can see the demo code and actually run it by running python chr_and_ord.py or python3 chr_and_ord.py.

Real World Applications

The real world applications for these functions are usually very specific. Sometimes they are used to handle low-level character compatibility layers for legacy systems. But one simpler usage that i've run into, is for working with xlsx files.

xlsx files are the default Microsoft Excel extension files, in these files workbooks are ordered by row (numbers) and columns (letters). So to iterate through a set of rows you would need 'column_letter row_number' for example: "A1" --> "A2" --> "A3" etc. For rows you can iterate using ints, but for columns since they are individual characters to iterate you can convert them using ord and then increment/decrement and convert back to the correct character with chr.

For example:

import xlsxwriter # Third part module for working with xlsx files

filename = "data.xlsx"

workbook = xlsxwriter.Workbook(filename) # Create new workbook for data.xlsx
worksheet = workbook.add_worksheet() # Add a worksheet to put data into

data = [1,2,3,4] # list to iterate over

column = ord("A") # Initializing column counter: in this case 65

for number in data:
    worksheet.write("{}1".format(chr(column)), data[number]) # Writes data to A1, then B1, then C1 etc.
    column += 1

workbook.close() # Close workbook file and write data

Files

chr_and_ord_demo.py
""" Description: A demo of the ord() and chr() functions in python\ for ASCII table reference, please visit http://www.asciitable.com/ """ __author__ = "Canadian Coding" # chr() takes an int and returns the corresponding ASCII character print(chr(65)) # prints 'A' print(chr(97)) # prints 'a' # ord() takes a one letter string and returns the corresponding ASCII number as an int print(ord("A")) # Prints 65 print(ord("a")) # Prints 97 def alphabet_by_ord(): """Goes through and prints the uppercase alphabet using ord() and chr() """ current_letter = ord('A') # Initialize to A for letter in range(1,27): #Iterates over all 27 letters of the alphabet print(chr(current_letter)) current_letter += 1 # Move to next letters ordinance def capitalize_by_ord(letter = "a"): """Converts lowercase characters to uppercase using ord() and chr() Returns: (str): A message with the upper and original case version of the letter """ uppercase_letter = ord(letter) - 32 # Decreasing letters ordinance by offset return "Letter {} converted to {}".format(letter, chr(uppercase_letter)) alphabet_by_ord() print(capitalize_by_ord())