Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this tutorial, we will learn how to convert between ASCII values and characters in Python.
To convert a character to its corresponding ASCII value, you can use the built-in ord()
function. The ord()
function takes a single character string as input and returns its Unicode code point, which is the same as the ASCII value for characters in the ASCII table.
# Convert a character to its ASCII value char = 'A' ascii_value = ord(char) print(f"The ASCII value of {char} is {ascii_value}")
Output:
The ASCII value of A is 65
To convert an ASCII value to the corresponding character, you can use the built-in chr()
function. The chr()
function takes an integer (the Unicode code point) as input and returns the corresponding character.
# Convert an ASCII value to a character ascii_value = 65 char = chr(ascii_value) print(f"The character corresponding to the ASCII value {ascii_value} is {char}")
Output:
The character corresponding to the ASCII value 65 is A
To convert a string of characters to a list of ASCII values, you can use a list comprehension along with the ord()
function.
input_string = "Hello" # Convert the string to a list of ASCII values ascii_values = [ord(char) for char in input_string] print(f"The ASCII values of the string '{input_string}' are {ascii_values}")
Output:
The ASCII values of the string 'Hello' are [72, 101, 108, 108, 111]
To convert a list of ASCII values to a string, you can use a list comprehension along with the chr()
function and then join the characters using the join()
method.
ascii_values = [72, 101, 108, 108, 111] # Convert the list of ASCII values to a string output_string = ''.join([chr(value) for value in ascii_values]) print(f"The string corresponding to the ASCII values {ascii_values} is '{output_string}'")
Output:
The string corresponding to the ASCII values [72, 101, 108, 108, 111] is 'Hello'
In this tutorial, we learned how to convert between characters and their ASCII values, and how to convert entire strings to and from lists of ASCII values in Python.
Python convert string to ASCII:
my_string = "Hello" ascii_values = [ord(char) for char in my_string] print(ascii_values) # [72, 101, 108, 108, 111]
Convert ASCII to string in Python:
ascii_values = [72, 101, 108, 108, 111] my_string = ''.join(chr(value) for value in ascii_values) print(my_string) # 'Hello'
ASCII character to integer conversion in Python:
ascii_char = 'A' ascii_value = ord(ascii_char) print(ascii_value) # 65
Convert integer to ASCII character in Python:
ascii_value = 65 ascii_char = chr(ascii_value) print(ascii_char) # 'A'
Python ASCII to hexadecimal conversion:
ascii_value = 65 hex_value = hex(ascii_value) print(hex_value) # '0x41'
Convert ASCII to binary in Python:
ascii_value = 65 binary_value = bin(ascii_value) print(binary_value) # '0b1000001'
Convert ASCII to Unicode in Python: (Unicode and ASCII values are often the same for basic characters)
ascii_value = 65 unicode_value = chr(ascii_value).encode('unicode_escape').decode() print(unicode_value) # '\\u0041'
Convert string to ASCII code points in Python:
my_string = "Hello" ascii_code_points = [ord(char) for char in my_string] print(ascii_code_points) # [72, 101, 108, 108, 111]
Python ASCII to decimal conversion: (Decimal is the same as ASCII for basic characters)
ascii_value = 65 decimal_value = ascii_value print(decimal_value) # 65
Encode and decode ASCII with base64 in Python:
import base64 my_string = "Hello" encoded_ascii = base64.b64encode(my_string.encode()) decoded_ascii = base64.b64decode(encoded_ascii).decode() print(encoded_ascii) # b'SGVsbG8=' print(decoded_ascii) # 'Hello'
Convert ASCII to lowercase in Python:
ascii_char = 'A' lowercase_ascii = chr(ord(ascii_char) + 32) print(lowercase_ascii) # 'a'
Convert ASCII to uppercase in Python:
ascii_char = 'a' uppercase_ascii = chr(ord(ascii_char) - 32) print(uppercase_ascii) # 'A'