Skip to content

Instantly share code, notes, and snippets.

@Lenochxd
Last active June 23, 2025 18:41
Show Gist options
  • Save Lenochxd/a001b1dc69d2addfd019c2465e69f920 to your computer and use it in GitHub Desktop.
Save Lenochxd/a001b1dc69d2addfd019c2465e69f920 to your computer and use it in GitHub Desktop.
T9 string to text and vice versa
t9_mapping = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz',
'0': ' '
}
input_string = input("Enter T9 input: ").split()
text = ""
for char in input_string:
if char[0] in t9_mapping:
text += t9_mapping[char[0]][len(char) - 1] # Get the character based on the number of presses
else:
text += char
print()
print(text)
t9_mapping = {
'abc': '2',
'def': '3',
'ghi': '4',
'jkl': '5',
'mno': '6',
'pqrs': '7',
'tuv': '8',
'wxyz': '9',
' ': '0'
}
input_string = input("Enter text input: ")
output = ""
for char in input_string:
for key, value in t9_mapping.items():
if char in key:
output += value * (key.index(char) + 1) + " " # Repeat the number based on the position of the character
break
else:
output += char # If character is not found in mapping, add it as is
print()
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment