omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    string.maketrans() method not working

    Pythonista
    help
    4
    7
    7474
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • craftmojo
      craftmojo last edited by

      Today, I tried to code in pythonista for ios, everything seems okay. But, when I use maketrans method , it not working at all. What happened? I tried the code in pycharm, it worked perfectly.

      Please help.

      1 Reply Last reply Reply Quote 0
      • craftmojo
        craftmojo last edited by

        This is my code:

        import string
        str = 'map.html'
        i = 'abcdefghijklmnopqrstuvwxyz'
        o = 'cdefghijklmnopqrstuvwxyzab'
        table = string.maketrans(i, o)
        print (str.translate(table))

        1 Reply Last reply Reply Quote 0
        • dgelessus
          dgelessus last edited by

          str is the name of Python's string type. In your code, you're overwriting it with another object. This is not a good idea (it means that you cannot use the str type properly anymore) but normally it would only affect your own code. Here in Pythonista it also breaks some internal print code. The solution is to choose a different name for your string - call it s or name or url or whatever you like.

          1 Reply Last reply Reply Quote 1
          • Phuket2
            Phuket2 last edited by

            I had just done this

            import string
            
            #str = 'map.html'
            s = 'map.html'
            i = 'abcdefghijklmnopqrstuvwxyz'
            o = 'cdefghijklmnopqrstuvwxyzab'
            
            table = string.maketrans(i, o)
            print str.translate(s, table)
            
            1 Reply Last reply Reply Quote 0
            • craftmojo
              craftmojo last edited by

              I see. Now, it worked perfectly.
              So, in pythonista, this word already taken.

              Many thanks!

              1 Reply Last reply Reply Quote 0
              • ccc
                ccc last edited by

                In Python this word is taken.

                1 Reply Last reply Reply Quote 3
                • dgelessus
                  dgelessus last edited by

                  Exactly. str is the string class, and you can use it to convert an object to a string. For example, str(42) is the same as '42'. This is important when you want to do something like this:

                  print("Your score is: " + str(42))
                  

                  Without using str, you would get a TypeError, because you can't add a string and a number.

                  This means that if you assign something different to the name str, you cannot use the original str class anymore. The same goes for other standard types like int, float, list, dict, etc.

                  1 Reply Last reply Reply Quote 3
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors