Name already in use
Work fast with our official CLI. Learn more about the CLI.
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
This python module can convert a single character or a string into the ASCII value of that character.
To use it, download the cti.py file, make sure it is in the same folder as the project you are working on and write import cti
cti.example() takes nothing; will show the basic functions you can do
cti.single() takes a string of one character; will return an integer
cti.multi() takes a string of any amount and a boolean; will return a list (array) if the boolean is False , and a more readable string if True
cti.prtMulti() takes a string of any amount; will return a list (array) and print a more readable string to the console.
cti.bin() takes a string of any amount; will return an array of binary values
NOTE: any characters given that are not in the accepted data will return a blank value (0 for single; nothing for multi)
How can I convert a character to a integer in Python, and viceversa?
For example, for the character a , I want to get 97 , and vice versa.
5 Answers 5
For long string you could use this.
![]()
Not OP’s question, but given the title How can I convert a character to a integer in Python ,
![]()
-
The Overflow Blog
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.6.8.43486
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to Convert Char to Int in Python
To convert a character to an integer in Python, you can use the built-in “ord()” function that returns the ASCII value of that character. For example, the ord(“a”) function returns 97. The ord() is a built-in function that accepts a character and returns the ASCII value of that character. It returns the number representing the Unicode code of a specified character.
Example 1: How to use the ord() function to convert a char to int
Let’s define a character and convert that character into an integer using the ord() function.
Output
That means the ASCII value of the K character is 75.
Example 2: Check if it is case-sensitive
The ASCII value of capital K is 75, but the ASCII value of small k is different.
Output
You can see that it’s case-sensitive. This is because capital and small characters have different ASCII values. Let’s take another character and convert it into an integer.
Output
Alternate method: Using int() to convert a character to an integer
You can use the int() function to convert a character to an integer. The int() function takes a single argument, the value to convert, and returns an integer.
The int() function only works for characters that are digits; if the character is not a digit, it will raise a ValueError exception.
In that case, use the ord() function instead of an int() function.
Example of using int() function
Output
Let’s pass the non-integer value to the int() function and see the output.
Output
And we get the ValueError because the interpreter cannot convert “K” into an integer.
How to convert int to char in Python
To convert int to char in Python, use the chr() function. The chr() is a built-in function that takes an integer as an argument and returns a string representing a character at that code point.
Output
In this example, we pass the ASCII value to the chr() function, converting it into a character.
The character value of 75 is K because the chr() function takes an ASCII value as an argument and converts it into a character value.
This is different from converting a string to an integer since we are here dealing with characters, not strings.
To convert string to int, use the int() function, and converting int to string, use the str() function.
Convert a Character to an Integer and Vice Versa in Python

This tutorial discusses methods to convert a character to an integer and an integer to a character in Python.
Use chr() to Convert an Integer to a Character in Python
Please enable JavaScript
We can use the built-in function chr() to convert an integer to its character representation in Python. The below example illustrates this.
It will result in an error if you provide an invalid integer value to this. For example:
Therefore, it is always good to put this code in a try. except block to catch the error, if any, and avoid any crash. The below example illustrates this:
Use ord() to Convert a Character to an Integer in Python
We can use the built-in function ord() to convert a character to an integer in Python. The below example illustrates this.
The above method also catches any invalid input and prints out the error instead of crashing the code. For example: