manipulating a string using ord()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Carlo Gambino
    New Member
    • Feb 2008
    • 12

    manipulating a string using ord()

    I'm trying to manipulate strings of user input data, using ord() to assign the decimal value to each character in the string. I can return the values, but need to understand the best way to store the decimal values, manipulate them, and return the new values to a new string.

    Any input or suggestions?
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by Carlo Gambino
    I'm trying to manipulate strings of user input data, using ord() to assign the decimal value to each character in the string. I can return the values, but need to understand the best way to store the decimal values, manipulate them, and return the new values to a new string.

    Any input or suggestions?

    My suggestion is stop posting so many threads and stick to one....

    I don't understand why you would want to do what you're talking about but here's a simple example...

    [CODE=python]
    >>> t = raw_input("Ente r string: ")
    Enter string: abcdef
    >>> list = []
    >>> for letter in t:
    >>> list.append(ord (letter))

    >>> list
    [97, 98, 99, 100, 101, 102]
    >>> # Do your data manip. here
    >>> list
    [102, 103, 104, 105, 106, 107]
    >>> t2 = ''
    >>> for elem in list:
    >>> t2 += chr(elem)

    >>> t2
    'fghijk'[/CODE]

    Comment

    • Carlo Gambino
      New Member
      • Feb 2008
      • 12

      #3
      Right, thanks! I'm just frustrated because I've been pouring over forums and books for weeks trying to get off the ground with python and I feel like it's more trouble at every turn.

      Thanks for the post! Your code helps more than I can express! You've let me see the light, thanks again!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        You can jumble up a string using the string module:[code=Python]import string

        s1 = string.ascii_lo wercase+string. digits
        s2 = '$qiwsd~52a6-gzjh%f9+30c78x! #*^<>;:|&'
        m1 = string.maketran s(s1, s2)
        m2 = string.maketran s(s2, s1)
        s3 = 'dpeopfgghcf234 8dfjm89gkl'.tra nslate(m1)
        s4 = s3.translate(m2 )
        print s3
        print s4[/code]

        >>> whsjhd~~5id*^<| wdag|&~6-
        dpeopfgghcf2348 dfjm89gkl
        >>>

        Comment

        • Carlo Gambino
          New Member
          • Feb 2008
          • 12

          #5
          well I'm almost there! I've found my way to the code that gets me (almost) where I want. There is only one small problem I don't seem to understand.

          Code:
          >>>t = raw_input("Enter string: ")
          >>>Enter string: aaaaa
          >>>list = []
          >>>for letter in t:
                      list.append(ord(letter))
          
          >>> list
          [97, 97, 97, 97, 97]
          
          >>> list2 = []
          >>> for i in range(len(list)+1)[1:]:
          	     list2.append(list[i-1]+i)
          
          >>>>>> list2
          [98, 99, 100, 101, 102]
          This is so close to what I want, but the desired output is:
          Code:
          97 98 99 100 101
          Thanks again for the help!

          Comment

          • Carlo Gambino
            New Member
            • Feb 2008
            • 12

            #6
            **Solved**

            In case anyone stumbles across curiosity on a smilar subject, here is the code that finally made this work for me:

            Code:
            >>> plaintext = 'aeiou'
            >>> list = [ord(letter) for letter in plaintext]
            >>> list
            [97, 101, 105, 111, 117]
            >>> list2 =[list[i-1]+i for i in range(len(list))]
            >>> list2
            [117, 98, 103, 108, 115]
            >>> import string
            >>> cyphertext = string.join([chr(c) for c in list2], '')
            >>> cyphertext
            'ubgls'

            Comment

            Working...