How to Convert Char to Int in Python

Here are two ways to convert a character to an integer in Python:

  1. ord(): It is used to get the ASCII value of a character.
  2. int(): It is used when a character is a digit.

For example, the ord(“a”) function returns 97.

Method 1: Using the ord()

Converting Char to Int in Python

Example 1: Basic Usage

# For ASCII value 
char = "K"

print(ord(char))

Output

75

Example 2: Check if is case-sensitive

The ASCII value of capital K is 75, but the small k’s value is different.

charData = "k"

print(ord(charData))

Output

107

Method 2: Using int()

Visual Representation of Using int() function

# For digit value
digit_char = '2'
print(int(digit_char))

Output

2

That’s it!

Related posts

Python string to int

Python string to json

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.