How do I replace all characters in a Python string?












-1














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'))









share|improve this question




















  • 2




    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










  • 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
















-1














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'))









share|improve this question




















  • 2




    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










  • 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














-1












-1








-1


1





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'))









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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






  • 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














  • 2




    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










  • 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








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












1 Answer
1






active

oldest

votes


















1














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...]))








share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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...]))








    share|improve this answer


























      1














      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...]))








      share|improve this answer
























        1












        1








        1






        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...]))








        share|improve this answer












        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...]))









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 22:51









        MegaBluejay

        35118




        35118






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ