π Day 42/150 – ASCII Value of a Character in Python
The ASCII value is the numeric code assigned to characters.
Example:
A = 65
a = 97
0 = 48
Let’s explore different ways to find ASCII value in Python π
πΉ Method 1 – Using ord()
ch = 'A'
print("ASCII Value:", ord(ch))✅ ord() returns the ASCII/Unicode value.πΉ Method 2 – Taking User Input
ch = input("Enter a character: ") print("ASCII Value:", ord(ch))
✅ Dynamic version.
πΉ Method 3 – Using Function
def get_ascii(ch): return ord(ch) print(get_ascii('a'))
✅ Reusable approach.
πΉ Method 4 – Using Loop for String
text = "ABC" for ch in text: print(ch, "=", ord(ch))
✅ Useful for multiple characters.
πΉ Method 5 – Using Dictionary Comprehension
chars = ['A', 'B', 'C'] ascii_values = {ch: ord(ch) for ch in chars} print(ascii_values)
✅ Great for storing multiple values.
πΉ Output
ASCII Value: 65
π₯ Key Takeaways
✔️ Use ord() to get ASCII value
✔️ Works for letters, digits, symbols
✔️ Useful in encoding problems
✔️ Can handle single or multiple characters


0 Comments:
Post a Comment