String Manipulation

Discussion in 'Python' started by lamar_air, Jul 15, 2003.

  1. lamar_air

    lamar_air Guest

    I need a piece of code that takes a string like this string1 =
    "aaa/bbb/ccc/dd" and extracts a string containting the character after
    the last "/"

    So for this example the result would be "dd"

    like this:
    for i=0; string1.right(i) != '/'; i++

    result = string1.mid(i, string1.length())

    but in python.
     
    lamar_air, Jul 15, 2003
    #1
    1. Advertising

  2. | I need a piece of code that takes a string like this
    | string1 = "aaa/bbb/ccc/dd" and extracts a string containting
    | the character after the last "/"
    |
    | So for this example the result would be "dd"
    | ...

    lamar_air ...

    Here is one way ...


    >>> str_in = 'aaa/bbb/ccc/dd'
    >>>
    >>> list_in = str_in.split( '/' )
    >>>
    >>> last_element = list_in[ -1 ]
    >>>
    >>> print last_element

    dd
    >>>



    --
    Cousin Stanley
    Human Being
    Phoenix, Arizona
     
    Cousin Stanley, Jul 15, 2003
    #2
    1. Advertising

  3. lamar_air

    Inyeol Lee Guest

    On Tue, Jul 15, 2003 at 01:58:09PM -0700, Cousin Stanley wrote:
    > | I need a piece of code that takes a string like this
    > | string1 = "aaa/bbb/ccc/dd" and extracts a string containting
    > | the character after the last "/"
    > |
    > | So for this example the result would be "dd"
    > | ...
    >
    > lamar_air ...
    >
    > Here is one way ...
    >
    >
    > >>> str_in = 'aaa/bbb/ccc/dd'
    > >>>
    > >>> list_in = str_in.split( '/' )
    > >>>
    > >>> last_element = list_in[ -1 ]
    > >>>
    > >>> print last_element

    > dd
    > >>>

    >
    >
    > --
    > Cousin Stanley
    > Human Being
    > Phoenix, Arizona
    >


    In Unix,

    >>> os.path.basename("aaa/bbb/ccc")

    'ccc'

    Inyeol
     
    Inyeol Lee, Jul 15, 2003
    #3
  4. John Hunter wrote:

    >>>>>>"lamar" == lamar air <> writes:

    >
    >
    > lamar> I need a piece of code that takes a string like this
    > lamar> string1 = "aaa/bbb/ccc/dd" and extracts a string
    > lamar> containting the character after the last "/"
    >
    > One good way to do this is to split the string on the '/' character,
    > which creates a list of strings
    >
    > >>> string1 = "aaa/bbb/ccc/dd"
    > >>> parts = string1.split('/')
    > >>> parts

    > ['aaa', 'bbb', 'ccc', 'dd']
    >
    > Now you just want to get the last element of this list; python allows
    > you to index with -1 to get the last element, -2 to get the second to
    > last, etc...
    >
    > >>> parts[-1]

    > 'dd'
    >
    > JDH
    >


    While is is perfectly acceptable, you might want to consider another
    solution if it is *path names* you are manipulating:

    >>> import os
    >>> os.path.split("aaa/bbb/ccc/dd")

    ('aaa/bbb/ccc', 'dd')
    >>> os.path.split("aaa/bbb/ccc/dd")[1]

    'dd'


    because this will work correctly on other platforms too when the
    path separator is not '/'.

    --Irmen de JOng
     
    Irmen de Jong, Jul 15, 2003
    #4
  5. lamar_air

    Brandon Beck Guest

    I know a few people replied to this already, but here's one additional
    possibility. If the data in string1 is a file path for your platform,
    then you can use the os.path module.

    >>> import os.path
    >>> os.path.split("aaa/bbb/ccc/dd")

    ('aaa/bbb/cc', 'dd')
    >>> os.path.splitext("filename.ext")

    ('filename', '.ext')

    The other suggestions will of course work, but if you data is a file
    path, then using the os.path module should be a more portable solution.

    Brandon



    "lamar_air" <> wrote in message
    news:...
    > I need a piece of code that takes a string like this string1 =
    > "aaa/bbb/ccc/dd" and extracts a string containting the character after
    > the last "/"
    >
    > So for this example the result would be "dd"
    >
    > like this:
    > for i=0; string1.right(i) != '/'; i++
    >
    > result = string1.mid(i, string1.length())
    >
    > but in python.
     
    Brandon Beck, Jul 15, 2003
    #5
  6. On 15 Jul 2003 13:23:09 -0700, (lamar_air) wrote:

    >I need a piece of code that takes a string like this string1 =
    >"aaa/bbb/ccc/dd" and extracts a string containting the character after
    >the last "/"
    >
    >So for this example the result would be "dd"
    >
    >like this:
    >for i=0; string1.right(i) != '/'; i++
    >
    >result = string1.mid(i, string1.length())
    >
    >but in python.


    Others have posted the split() solutions.
    You could also search backward in Python:

    >>> s = "aaa/bbb/ccc/dd"
    >>> s[s.rfind('/')+1:]

    'dd'
    >>> s='no_slashes'
    >>> s[s.rfind('/')+1:]

    'no_slashes'

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 16, 2003
    #6
  7. On 15 Jul 2003 19:51:56 -0700, (Vinoo Vasudevan) wrote:

    > (lamar_air) wrote in message news:<>...
    >> I need a piece of code that takes a string like this string1 =
    >> "aaa/bbb/ccc/dd" and extracts a string containting the character after
    >> the last "/"
    >>
    >> So for this example the result would be "dd"
    >>
    >> like this:
    >> for i=0; string1.right(i) != '/'; i++
    >>
    >> result = string1.mid(i, string1.length())
    >>
    >> but in python.

    >
    >How about this:
    >string1 = 'aaa/bbb/ccc/dd'
    >result = string1[string1.rfind('/')+1:]
    >
    >Hope it's helpful,

    Sorry Vinoo, for some reason your post did not show up for me before I posted
    the same solution, even though your post is dated much before mine.
    I guess it has to do with delays in news servers forwarding and such.

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 16, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jared in ecs

    Perl string manipulation

    jared in ecs, Oct 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    823
    Roy Johnson
    Oct 22, 2003
  2. Garfield

    String Array Manipulation Problem

    Garfield, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,836
    Michael Lang
    Aug 27, 2003
  3. Aaron

    String Manipulation

    Aaron, Dec 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,664
    Dave Davidson
    Dec 31, 2003
  4. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    860
  5. Chris

    String manipulation

    Chris, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    466
    Pete Wright
    May 24, 2004
Loading...

Share This Page