How do I replace all characters in a Python string?
I found a solution on stackoverflow but it doesn't seem to work. I have made a string scanner that checks for character frequency and then replaces all characters with the "real" characters. I've made sure that the character recognition works but when I try replacing all characters in a string they no longer match up with the expected/calculated characters (when I try replacing for example only 2 characters it works fine and matches up perfectly). Here is my replacement code:
print(text.replace(re,'e').replace(rt,'t').replace(ra,'a').replace(ro,'o').replace(ri,'i').replace(rn,'n').replace(rs,'s').replace(rr,'r').replace(rh,'h').replace(rl,'l').replace(ru,'u').replace(rc,'c').replace(rm,'m').replace(rf,'f').replace(ry,'y').replace(rw,'w').replace(rg,'g').replace(rp,'p').replace(rb,'b').replace(rv,'v').replace(rk,'k').replace(rx,'x').replace(rq,'q').replace(rj,'j').replace(rz,'z').replace(rd,'d'))
python python-3.x
|
show 2 more comments
I found a solution on stackoverflow but it doesn't seem to work. I have made a string scanner that checks for character frequency and then replaces all characters with the "real" characters. I've made sure that the character recognition works but when I try replacing all characters in a string they no longer match up with the expected/calculated characters (when I try replacing for example only 2 characters it works fine and matches up perfectly). Here is my replacement code:
print(text.replace(re,'e').replace(rt,'t').replace(ra,'a').replace(ro,'o').replace(ri,'i').replace(rn,'n').replace(rs,'s').replace(rr,'r').replace(rh,'h').replace(rl,'l').replace(ru,'u').replace(rc,'c').replace(rm,'m').replace(rf,'f').replace(ry,'y').replace(rw,'w').replace(rg,'g').replace(rp,'p').replace(rb,'b').replace(rv,'v').replace(rk,'k').replace(rx,'x').replace(rq,'q').replace(rj,'j').replace(rz,'z').replace(rd,'d'))
python python-3.x
2
Has nothing to do with the Cryptography tag.
– kelalaka
Nov 11 at 22:47
Look atstr.translate
andstr.maketrans
– Michael Butscher
Nov 11 at 22:49
Each call to replace is operating on the result of the previous. By the time you get to the 14th call, you are guaranteed to be replacing letters that you’ve already replaced. Use .translate as mentioned in the solution below.
– soundstripe
Nov 11 at 23:02
1
Arere
/rt
/ra
etc. single characters, or multicharacter strings?str.translate
can only replace based on matches of a single character (the replacement string can be any length).
– ShadowRanger
Nov 11 at 23:07
On a side note... ifre
/rt
/ra
etc... are the real characters... it looks like your arguments to replace are the wrong way around anyway...
– Jon Clements♦
Nov 11 at 23:23
|
show 2 more comments
I found a solution on stackoverflow but it doesn't seem to work. I have made a string scanner that checks for character frequency and then replaces all characters with the "real" characters. I've made sure that the character recognition works but when I try replacing all characters in a string they no longer match up with the expected/calculated characters (when I try replacing for example only 2 characters it works fine and matches up perfectly). Here is my replacement code:
print(text.replace(re,'e').replace(rt,'t').replace(ra,'a').replace(ro,'o').replace(ri,'i').replace(rn,'n').replace(rs,'s').replace(rr,'r').replace(rh,'h').replace(rl,'l').replace(ru,'u').replace(rc,'c').replace(rm,'m').replace(rf,'f').replace(ry,'y').replace(rw,'w').replace(rg,'g').replace(rp,'p').replace(rb,'b').replace(rv,'v').replace(rk,'k').replace(rx,'x').replace(rq,'q').replace(rj,'j').replace(rz,'z').replace(rd,'d'))
python python-3.x
I found a solution on stackoverflow but it doesn't seem to work. I have made a string scanner that checks for character frequency and then replaces all characters with the "real" characters. I've made sure that the character recognition works but when I try replacing all characters in a string they no longer match up with the expected/calculated characters (when I try replacing for example only 2 characters it works fine and matches up perfectly). Here is my replacement code:
print(text.replace(re,'e').replace(rt,'t').replace(ra,'a').replace(ro,'o').replace(ri,'i').replace(rn,'n').replace(rs,'s').replace(rr,'r').replace(rh,'h').replace(rl,'l').replace(ru,'u').replace(rc,'c').replace(rm,'m').replace(rf,'f').replace(ry,'y').replace(rw,'w').replace(rg,'g').replace(rp,'p').replace(rb,'b').replace(rv,'v').replace(rk,'k').replace(rx,'x').replace(rq,'q').replace(rj,'j').replace(rz,'z').replace(rd,'d'))
python python-3.x
python python-3.x
edited Nov 11 at 22:48
coldspeed
119k19115193
119k19115193
asked Nov 11 at 22:45
Malik
215
215
2
Has nothing to do with the Cryptography tag.
– kelalaka
Nov 11 at 22:47
Look atstr.translate
andstr.maketrans
– Michael Butscher
Nov 11 at 22:49
Each call to replace is operating on the result of the previous. By the time you get to the 14th call, you are guaranteed to be replacing letters that you’ve already replaced. Use .translate as mentioned in the solution below.
– soundstripe
Nov 11 at 23:02
1
Arere
/rt
/ra
etc. single characters, or multicharacter strings?str.translate
can only replace based on matches of a single character (the replacement string can be any length).
– ShadowRanger
Nov 11 at 23:07
On a side note... ifre
/rt
/ra
etc... are the real characters... it looks like your arguments to replace are the wrong way around anyway...
– Jon Clements♦
Nov 11 at 23:23
|
show 2 more comments
2
Has nothing to do with the Cryptography tag.
– kelalaka
Nov 11 at 22:47
Look atstr.translate
andstr.maketrans
– Michael Butscher
Nov 11 at 22:49
Each call to replace is operating on the result of the previous. By the time you get to the 14th call, you are guaranteed to be replacing letters that you’ve already replaced. Use .translate as mentioned in the solution below.
– soundstripe
Nov 11 at 23:02
1
Arere
/rt
/ra
etc. single characters, or multicharacter strings?str.translate
can only replace based on matches of a single character (the replacement string can be any length).
– ShadowRanger
Nov 11 at 23:07
On a side note... ifre
/rt
/ra
etc... are the real characters... it looks like your arguments to replace are the wrong way around anyway...
– Jon Clements♦
Nov 11 at 23:23
2
2
Has nothing to do with the Cryptography tag.
– kelalaka
Nov 11 at 22:47
Has nothing to do with the Cryptography tag.
– kelalaka
Nov 11 at 22:47
Look at
str.translate
and str.maketrans
– Michael Butscher
Nov 11 at 22:49
Look at
str.translate
and str.maketrans
– Michael Butscher
Nov 11 at 22:49
Each call to replace is operating on the result of the previous. By the time you get to the 14th call, you are guaranteed to be replacing letters that you’ve already replaced. Use .translate as mentioned in the solution below.
– soundstripe
Nov 11 at 23:02
Each call to replace is operating on the result of the previous. By the time you get to the 14th call, you are guaranteed to be replacing letters that you’ve already replaced. Use .translate as mentioned in the solution below.
– soundstripe
Nov 11 at 23:02
1
1
Are
re
/rt
/ra
etc. single characters, or multicharacter strings? str.translate
can only replace based on matches of a single character (the replacement string can be any length).– ShadowRanger
Nov 11 at 23:07
Are
re
/rt
/ra
etc. single characters, or multicharacter strings? str.translate
can only replace based on matches of a single character (the replacement string can be any length).– ShadowRanger
Nov 11 at 23:07
On a side note... if
re
/rt
/ra
etc... are the real characters... it looks like your arguments to replace are the wrong way around anyway...– Jon Clements♦
Nov 11 at 23:23
On a side note... if
re
/rt
/ra
etc... are the real characters... it looks like your arguments to replace are the wrong way around anyway...– Jon Clements♦
Nov 11 at 23:23
|
show 2 more comments
1 Answer
1
active
oldest
votes
You might want to take a look at translate. Your code would probably look something like
text = text.translate(str.maketrans('abcd...', ''.join([ra, rb, rc, rd...]))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253997%2fhow-do-i-replace-all-characters-in-a-python-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You might want to take a look at translate. Your code would probably look something like
text = text.translate(str.maketrans('abcd...', ''.join([ra, rb, rc, rd...]))
add a comment |
You might want to take a look at translate. Your code would probably look something like
text = text.translate(str.maketrans('abcd...', ''.join([ra, rb, rc, rd...]))
add a comment |
You might want to take a look at translate. Your code would probably look something like
text = text.translate(str.maketrans('abcd...', ''.join([ra, rb, rc, rd...]))
You might want to take a look at translate. Your code would probably look something like
text = text.translate(str.maketrans('abcd...', ''.join([ra, rb, rc, rd...]))
answered Nov 11 at 22:51
MegaBluejay
35118
35118
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253997%2fhow-do-i-replace-all-characters-in-a-python-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Has nothing to do with the Cryptography tag.
– kelalaka
Nov 11 at 22:47
Look at
str.translate
andstr.maketrans
– Michael Butscher
Nov 11 at 22:49
Each call to replace is operating on the result of the previous. By the time you get to the 14th call, you are guaranteed to be replacing letters that you’ve already replaced. Use .translate as mentioned in the solution below.
– soundstripe
Nov 11 at 23:02
1
Are
re
/rt
/ra
etc. single characters, or multicharacter strings?str.translate
can only replace based on matches of a single character (the replacement string can be any length).– ShadowRanger
Nov 11 at 23:07
On a side note... if
re
/rt
/ra
etc... are the real characters... it looks like your arguments to replace are the wrong way around anyway...– Jon Clements♦
Nov 11 at 23:23