String Manipulation

John Hunter jdhunter at ace.bsd.uchicago.edu
Tue Jul 15 16:55:39 EDT 2003


>>>>> "lamar" == lamar air <lamar_air at hotmail.com> 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





More information about the Python-list mailing list