-4

if I have a list like this:

list1 = ['a', 'b', 'c', 'd']

How can I convert them to characters so I can get the ASCII codes?

Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64

3 Answers3

0

The ord function built-in to Python will convert the character into its corresponding ascii code.

codes = []
for i in list1:
    codes.append(ord(i)) # convert character into an integer code

# codes will be [97,98,99,100]
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
0

With a list comprehension

codes = [ord(char) for char in list1]

From ord() doc:

Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string.

Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
0
asciiCodes = [ord(x) for x in list1]
mfred
  • 177
  • 6