Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to Find ASCII Value of Character using Python?
The ord() function in Python returns the ASCII (ordinal) value of a character. This is useful for character encoding, sorting operations, and working with character data.
Syntax
ord(character)
Parameters:
- character ? A single Unicode character
Return Value: Returns an integer representing the ASCII/Unicode code point of the character.
Finding ASCII Value of a Single Character
char = 'A'
ascii_value = ord(char)
print(f"ASCII value of '{char}' is {ascii_value}")
ASCII value of 'A' is 65
Finding ASCII Values of Multiple Characters
You can iterate through a string to get ASCII values of all characters ?
text = "Hello"
for char in text:
print(f"'{char}' = {ord(char)}")
'H' = 72 'e' = 101 'l' = 108 'l' = 108 'o' = 111
Creating ASCII Value Dictionary
You can create a dictionary mapping characters to their ASCII values ?
text = "Python"
ascii_dict = {char: ord(char) for char in text}
print(ascii_dict)
{'P': 80, 'y': 121, 't': 116, 'h': 104, 'o': 111, 'n': 110}
Common ASCII Values
| Character | ASCII Value | Description |
|---|---|---|
| '0' - '9' | 48 - 57 | Digits |
| 'A' - 'Z' | 65 - 90 | Uppercase letters |
| 'a' - 'z' | 97 - 122 | Lowercase letters |
Conclusion
The ord() function is essential for converting characters to their ASCII values in Python. Use it for character analysis, encoding operations, and implementing custom sorting algorithms.
Advertisements
