Convert input string contain font in list
Question:
I want to convert a input text " " contain in the font list "original":
text = input("Enter text: ")
#Enter text:
original = [" ",
" 0123456789",
" ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"]
replace = ["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"]
originalMap = original[0]
replaceMap = replace[0]
result = ""
for i in text:
if i in originalMap:
result += replaceMap[originalMap.index(i)]
else:
result += i
print("Converted: " +result)
#Converted: APPLE
But I can’t convert with another font in the list "original" such as , , …
Could anyone help me on this?
Answers:
You need to add all the possible mappings in one structure, from each letter of the 4 fonts to the classic one
Use a dict for that. And give 4 times the mapping: one font > classic font, that will make a dict of 62*4=248 mappings
text = "A ! "
original = [" ",
" 0123456789",
" ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"]
replaceAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
originalMap = {}
for alphabet in original:
originalMap.update(dict(zip(alphabet, replaceAlphabet)))
Then for the use, use dict.get
, it’ll search for the value in the dict, if it don’t find it it’ll use the default value, set char
too for the default value => pick the corresponding value OR use the key itself
result = "".join(originalMap.get(char, char) for char in text)
print("Converted: " + result)
# Converted: APPLE!012
You can use the str.translate
function to replace characters:
original = [" ",
" 0123456789",
" ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"]
# This can just be a string, no need for it to be a list of strings
replace = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
# Create a mapping from char codes to the replacment
mapping = {ord(char):replace[index] for font in original for index,char in enumerate(font)}
# Use the translate method to modify each character according to the map
print("Converted:", input().translate(mapping))
See the documentation to learn more about str.translate
I think you’re hoping to convert the input to each of three different fonts. In which case:
original = " "
alternative = [
" ",
" ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
]
text = ' '
for a in alternative:
t = ''
for c in text:
if (i := original.find(c)) >= 0:
t += a[i]
else:
t += c
print(t)
Output:
APPLE
I want to convert a input text " " contain in the font list "original":
text = input("Enter text: ")
#Enter text:
original = [" ",
" 0123456789",
" ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"]
replace = ["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"]
originalMap = original[0]
replaceMap = replace[0]
result = ""
for i in text:
if i in originalMap:
result += replaceMap[originalMap.index(i)]
else:
result += i
print("Converted: " +result)
#Converted: APPLE
But I can’t convert with another font in the list "original" such as , , …
Could anyone help me on this?
You need to add all the possible mappings in one structure, from each letter of the 4 fonts to the classic one
Use a dict for that. And give 4 times the mapping: one font > classic font, that will make a dict of 62*4=248 mappings
text = "A ! "
original = [" ",
" 0123456789",
" ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"]
replaceAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
originalMap = {}
for alphabet in original:
originalMap.update(dict(zip(alphabet, replaceAlphabet)))
Then for the use, use dict.get
, it’ll search for the value in the dict, if it don’t find it it’ll use the default value, set char
too for the default value => pick the corresponding value OR use the key itself
result = "".join(originalMap.get(char, char) for char in text)
print("Converted: " + result)
# Converted: APPLE!012
You can use the str.translate
function to replace characters:
original = [" ",
" 0123456789",
" ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"]
# This can just be a string, no need for it to be a list of strings
replace = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
# Create a mapping from char codes to the replacment
mapping = {ord(char):replace[index] for font in original for index,char in enumerate(font)}
# Use the translate method to modify each character according to the map
print("Converted:", input().translate(mapping))
See the documentation to learn more about str.translate
I think you’re hoping to convert the input to each of three different fonts. In which case:
original = " "
alternative = [
" ",
" ",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
]
text = ' '
for a in alternative:
t = ''
for c in text:
if (i := original.find(c)) >= 0:
t += a[i]
else:
t += c
print(t)
Output:
APPLE