0

When we need to slice a string at a particular location, we need to know the index from where we want to.

For example, in the string:

>>> s = 'Your ID number is: 41233'

I want to slice the string starting from : and get the number.

Sure I can count at what index : is and then slice, but is that really a good approach?

Of course I can do a s.index(':'). But that would be an extra step, so I came up with something like:

>>> print s[(s.index(':')+2):]
41233

But somehow I don't like the looks of it.

So my question is, given a long string which you want to slice, how do you find the index from where to begin the slicing in the easiest and most readable way? If there is a trick to do it orally, I would love to know that.

Jelle De Loecker
  • 20,999
  • 27
  • 100
  • 142
user225312
  • 126,773
  • 69
  • 172
  • 181

6 Answers6

2

Perhaps you could use split():

>>> s = 'Your ID number is: 41233'
>>> print s.split(":")[1].strip()
41233
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Interesting. This does seem a lot better. – user225312 Dec 12 '10 at 19:24
  • I would probably do it like this, if I was ever worried about the delimiter not existing: `print s.split(":")[-1].strip()` Otherwise, I'd put it in a try clause. Also, if there were somehow 2 delimiters, this will give you the last group of the split. – jgritty Dec 12 '10 at 20:54
2
text, sep, number = 'Your ID number is: 41233'.partition(':')
print number

works too. But it won't fail if the separator is not in the string.

That unpacking works for split too:

text, number = 'Your ID number is: 41233'.split(':',1)
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
1

Another approach is 'Your ID number is: 41233'.split(':')[1].strip().

khachik
  • 28,112
  • 9
  • 59
  • 94
0

Recently came across partition

string = "Your ID number is: 41233"

string = string.partition(':')

print string[2]
Mike Anson
  • 77
  • 5
0

So my question is, given a long string which you want to slice, how do you find the index from where to begin the slicing in the easiest and most readable way?

When "where to begin the slicing" is a specific symbol, you don't; instead you just as Python to split the string up with that symbol as a delimiter, or partition it into the bits before/within/after the symbol, as in the other answers. (split can split the string into several pieces if several delimiters are found; partition will always give three pieces even if the symbol is not there at all.)

If there is a trick to do it orally, I would love to know that.

I really don't think you mean "orally". :)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0

I wouldn't use slicing at all unless there's some other compelling reason you want to do so. Instead, this sounds like a perfect job for re the regular expression module in the standard library. Here's an example of using it to solve your problem:

import re
compile_obj = re.compile(r'Your ID number is:\s(?P<ID>\d+)')

s = 'Your ID number is: 41233'
match_obj = compile_obj.search(s)
if match_obj:
    print match_obj.group('ID')
# 41233
martineau
  • 119,623
  • 25
  • 170
  • 301