Reputation: 11
Sorry if this is a really simple question but I'm very new to python and I'm confused as to where to even start with this.
What I need to do is substitute at least 5 letters from a sentence to produce a new encrypted sentence i.e "Hello my name is TGMezza"
could be encrypted using ('H':'1', 'e':'2', 'l':'3', 'o':'4', 'm':'5')
so it would instead read "12334 5y n15e is TGM2zza"
. I know this is probably a really simple question but I'd appreciate as much explanation of how to do this as possible because I'm such a newbie to this and all I've been able to find on my searches are much more complicated forms of encryption like a caesar cipher.
Upvotes: 0
Views: 492
Reputation: 1875
replacements = { 'H':'1', 'e':'2', 'l':'3', 'o':'4', 'm':'5' }
s = "Hello my name is TGMezza"
for key in replacements:
s = s.replace(key,replacements[key])
print(s)
Or
replacements = { 'H':'1', 'e':'2', 'l':'3', 'o':'4', 'm':'5' }
s = "Hello my name is TGMezza"
s = ''.join(replacements[x] if x in replacements else x for x in s)
Upvotes: 2
Reputation: 1133
You can use str.translate
method to translate each character into another character:
translation = str.maketrans("Hello", "12345") # Creates the table to convert "H" to "1" etc.
# or:
tranlation = str.maketrans({'H':'1', 'e':'2', 'l':'3', 'o':'4', 'm':'5'})
text = "Hello my name is TGMezza"
print(text.translate(translation))
Upvotes: 1