Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Python- manipulating a string using ord()

  1. #1
    Join Date
    Jul 2006
    Location
    Ohio
    Beans
    429
    Distro
    Ubuntu 6.06

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

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Python- manipulating a string using ord()

    Quote Originally Posted by sp0nge View Post
    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?
    Takes a string, and returns a list of the values of each letter.
    PHP Code:
    def ord2(string):
        
    ord2 = []
        for 
    letter in string:
            
    ord2.append(ord(letter))
        return 
    ord2 
    Last edited by LaRoza; February 20th, 2008 at 09:08 PM.

  3. #3
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Python- manipulating a string using ord()

    Quote Originally Posted by LaRoza View Post
    Takes a string, and returns a list of the values of each letter.
    PHP Code:
    def ord2(string):
        
    ord2 = []
        for 
    letter in string:
            
    ord2.append(ord(letter))
        
    retrun ord2 
    O O, good place to drop a list comprehension:

    Code:
    def ord2(string):
        return [ord(letter) for letter in string]
    EDIT:

    Also, naming the variable the same as the function might cause some confusion

  4. #4
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Python- manipulating a string using ord()

    Quote Originally Posted by Wybiral View Post
    O O, good place to drop a list comprehension:

    Code:
    def ord2(string):
        return [ord(letter) for letter in string]
    EDIT:

    Also, naming the variable the same as the function might cause some confusion
    Thanks, I will have to investige these "list comprehensions" some more. (They are new to me)

    Not to mention I misspelled "return" (fixed it)

    I think declaring a local variable in a function to be the functions name is a good idea, if the language permits it. It makes sense to have the return value of the function be the function's name.

    (You can guess what language I learned that from...)

  5. #5
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Python- manipulating a string using ord()

    Quote Originally Posted by LaRoza View Post
    Thanks, I will have to investige these "list comprehensions" some more. (They are new to me)

    Not to mention I misspelled "return" (fixed it)

    I think declaring a local variable in a function to be the functions name is a good idea, if the language permits it. It makes sense to have the return value of the function be the function's name.

    (You can guess what language I learned that from...)
    List comprehensions are one of my favorite features in Python. You can do some really neat stuff with them (without having to set up a bunch of loops and be stuck with variables allocated outside the loops).

    As far as the variables being named after the function, I see what you're saying, it's just that when I look at it I feel like the function is returning itself (which makes me uncomfortable for some reason).

  6. #6
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Python- manipulating a string using ord()

    Quote Originally Posted by Wybiral View Post
    As far as the variables being named after the function, I see what you're saying, it's just that when I look at it I feel like the function is returning itself (which makes me uncomfortable for some reason).
    I typically use that convention. As popular as OO is, I prefer to think according to structured programming first. The variable name is only returned once, and is the only return in the function. Being that the variable is never used for anything else, it keeps the code readable and structured.

    (The whole convention I use is from Fortran)

  7. #7
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: Python- manipulating a string using ord()

    Here is an example of a function that uses ord to convert each character to an integer, adds one to the integer, and puts the result back into a string:

    Code:
    import string
    
    def add1(s):
        """
        Create a new string in which the ord value of each character is
        increased by one.
        """
        z = [chr(ord(x)+1) for x in s]
        return string.join(z,'')
    chr is used to convert the integer to a character, and string.join builds the string from the array of characters.

  8. #8
    Join Date
    Jul 2006
    Location
    Ohio
    Beans
    429
    Distro
    Ubuntu 6.06

    Re: Python- manipulating a string using ord()

    Ok so there is a lot of great information here, but I'm still new to this so it's a bit over my head.

    Code:
    z = [chr(ord(x)+1) for x in s]
        return string.join(z,'')
    This sounds like we're on the same page. What I don't understand is how to associate 'z' with the sting entered by the user- Yes I do. Thanks for the input. Helped me a lot!
    Last edited by sp0nge; February 20th, 2008 at 10:28 PM.

  9. #9
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Python- manipulating a string using ord()

    Quote Originally Posted by sp0nge View Post
    Ok so there is a lot of great information here, but I'm still new to this so it's a bit over my head.

    Code:
    z = [chr(ord(x)+1) for x in s]
        return string.join(z,'')
    This sounds like we're on the same page. What I don't understand is how to associate 'z' with the sting entered by the user.
    We are writting functions to do the task. To use mine (for example):

    PHP Code:
    ord2(raw_input("Enter string: ")) 
    Assuming the definition of ord2 is in the file (or imported), s will be the list of numbers.

  10. #10
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Python- manipulating a string using ord()

    Code:
    >>> s="string"
    >>> map(ord,s)
    [115, 116, 114, 105, 110, 103]
    >>>

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •