Learn Programming and technology with ease @ developerpublish.com

HomePythonPython Program to Find the ASCII Value of a Character

# Python Program to Find the ASCII Value of a Character

In this python tutorial, you will learn how to find the ASCII Value of a character using the ord function, print function and input function of the python programming language.

The ASCII is short formÂ  for American Standard Code for Information Interexchange, ASCII is a standard that assigns letters, numbers, and other characters in the 256 slots available in the 8-bit code.

## How to Find the ASCII Value of a Character in Python?

Letâ€™s take a look at the first source code, here the character is assigned in the code and the ord function is carried out.

```# Program to find the ASCII value of the given character

c = 'g'
print("The ASCII value of '" + c + "' is", ord(c))
```

OUTPUT:

`The ASCII value of 'g' is 103`

• At the start, we declareÂ the variableÂ  `c`Â  and assign it with the character `'g'` .
• Now, we display the output value using the `print` and display the statement/string as `""The ASCII value of '" + c + "' is", ord(c).`
• In the above statement, the variables `"+ c +"` will hold the ASCII value where the `ord(c)` function converts the given string of length one and returns an integer representing the unicode code point of the character. In simpler terms, it converts a character to an integer.

Letâ€™s take a look at the second source code , here the character is given as input by the user in the code and the ord function is carried out.

```# Program to find the ASCII value of the given character

c = input('Enter the character: ')
print("\n The ASCII value of '" + c + "' is", ord(c))
```

INPUT:

`%`

OUTPUT:

```Enter the character:
The ASCII value of '%' is 37```

1. Here we give the user the option to enter the values and the input values are scanned using the `input` function which is Â declared with the variableÂ  `c` .
2. In the STDIN section of the code editor the input values are entered.
3. Then we display the output value using the `print` and display the statement/string `""The ASCII value of '" + c + "' is", ord(c).`
4. As given in the first program,Â the variables `"+ c +"` will hold the ASCII value where the `ord()` function converts a character to an integer.

NOTE:

1. The ord function converts a character into its Unicode code value. This method accepts a single character and it consists of alphabets and symbols.
2. The print statement is followed by a comma, to initiate the ord function.
3. The print statement/string to be displayed in enclosed in double quotes.
4. The statement for the input function are enclosed in single quotes and parenthesis.
5. The \n in the code indicates a new line or the end of a statement line or a string.
6. For the ord function to be carried, the â€˜câ€™ variable is enclosed in + on both ends within double quotes. At the end of the print statement, it will hold the assigned variable (ord(c)).

### You May Also Like

#### Python Program to Remove Duplicates from a Linked List

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...

#### Python Program to Solve the Celebrity Problem

This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...

#### Python Program to Solve n-Queen Problem with Recursion

This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...