Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Accept string input and display its ASCII values - Python Program

To accept a string input and display its ASCII values in Python, the ord() function can be used. This function returns the Unicode code point of a character, which for standard ASCII characters is equivalent to their ASCII value.

Type 1 Program

# Accept string input from the user text = input("Enter a string: ") print(f"ASCII values for '{text}':") # Iterate through each character in the string for char in text: # Get the ASCII value of the character using ord() ascii_value = ord(char) # Display the character and its corresponding ASCII value print(f"Character: '{char}', ASCII Value: {ascii_value}")

Output

 
OUTPUT  :
Enter a string: Hello
ASCII values for 'Hello':
Character: 'H', ASCII Value: 72
Character: 'e', ASCII Value: 101
Character: 'l', ASCII Value: 108
Character: 'l', ASCII Value: 108
Character: 'o', ASCII Value: 111
    

Type 2 Program

# Accept string input from the user text = input("Enter a string: ") print(f"ASCII values for '{text}':") # Iterate through each character in the string for char in text: print(f"{char} -> {ord(char)}")

Output

 
OUTPUT  :
Enter a string: HELLO
ASCII values for 'HELLO':
H -> 72
E -> 69
L -> 76
L -> 76
O -> 79