0

I know it's simple, but I want a nice method. How from url string like:

/en/something/123/foo/

get

/something/123/foo/

?

It means, I want to remove first level (prefix).

santino
  • 159
  • 2
  • 11
  • [This should answer your question](http://stackoverflow.com/questions/1038824/python-strip-a-string) – maxm Aug 29 '11 at 08:20

2 Answers2

4

If it's always three characters long:

str = str[3:]

If it can be variable-length, using only string/list operations:

str = '/' + '/'.join( str.split('/')[2:] )

Or using regex:

import re
str = re.sub('^/[a-z]*', '', str)

I would probably use the last method. If you need to do this a lot (e.g. in a loop), consider compiling the regex using re.compile() to speed it up.

richardolsson
  • 4,041
  • 1
  • 17
  • 19
0

Building on richardolsson's answer, we can utilize the maxsplit parameter of str.split:

str = str.split("/", 2)[2]

This splits only the first two separators, and returns the rest (the third list item) unchanged.

emu
  • 1,597
  • 16
  • 20