Skip to content

Latest commit

 

History

History
executable file
·
43 lines (28 loc) · 850 Bytes

File metadata and controls

executable file
·
43 lines (28 loc) · 850 Bytes

String translation

Examples in Python 3

# Need to import module to get access to string
import string

"""
Translate some characters into numbers
"""
# Create a variable string with all numbers 0-9
nrs = string.digits
# Create a string which containts characters for each number in order
# 		 0123456789
chars = "olzeasgtbp"

# Create a translation table, from and to as parameters
trans = str.maketrans(chars, nrs)

# Create a string to translate
my_str = "hello world"
# Use translate() and the table created to translate string
x = my_str.translate(trans)
print(x)

Reference and read more

string — Common string operations

Revision history

2014-07-17 (sylvanas) PA1 First try.