Vilva
Vilva

Reputation: 811

How to find the ascii values of the characters in string?

I'm trying to convert characters in string into ascii value in Python

Like ord('a[i]') but ord expects only character not string!!

Is there any other method to solve this problem in python .

Upvotes: 2

Views: 5634

Answers (2)

Fred Larson
Fred Larson

Reputation: 62123

An alternative to Sven's answer:

[ord(c) for c in s]

... or the corresponding generator expression:

(ord(c) for c in s)

Upvotes: 3

Sven Marnach
Sven Marnach

Reputation: 602705

>>> s = "string"
>>> map(ord, s)
[115, 116, 114, 105, 110, 103]

Upvotes: 9

Related Questions