Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions conversions/ascii_to_char.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Convert a given ASCII value to its corresponding character.
"""


def ascii_to_char(ascii_value: int) -> str:
"""
Converts an ASCII integer value to its character equivalent.

>>> ascii_to_char(65)
'A'
>>> ascii_to_char(97)
'a'
>>> ascii_to_char(48)
'0'
"""
return chr(ascii_value)


if __name__ == "__main__":
import doctest

doctest.testmod()

input_value = input("Enter an ASCII value to convert to a character: ").strip()
try:
ascii_val = int(input_value)
if 0 <= ascii_val <= 127:
print(
f"The character for ASCII value {ascii_val} is: {ascii_to_char(ascii_val)}"

Check failure on line 30 in conversions/ascii_to_char.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

conversions/ascii_to_char.py:30:89: E501 Line too long (91 > 88)
)
else:
print("Please enter a valid ASCII value (0-127).")
except ValueError:
print("Invalid input. Please enter an integer.")
Loading