2

I have a string:

v = "1 - 5 of 5"

I would like only the part after 'of' (5 in the above) and strip everything before 'of', including 'of'?

The problem is that the string is not fixed as in it could be '1-100 of 100', so I can't specify to strip everything off after the 10 or so characters. I need to search for 'of' then strip everything off.

4 Answers 4

7

Using the partition method is most readable for these cases.

string = "1 - 5 of 5"
first_part, middle, last_part = string.partition('of')

result = last_part.strip()
Sign up to request clarification or add additional context in comments.

Comments

4

str.partition() was built for exactly this kind of problem:

In [2]: v = "1 - 5 of 5"

In [3]: v.partition('of')
Out[3]: ('1 - 5 ', 'of', ' 5')

In [4]: v.partition('of')[-1]
Out[4]: ' 5'

Comments

0
In [19]: v[v.rfind('of')+2:]
Out[19]: ' 5'

Comments

-1
p = find(v, "of")

gives you the position of "of" in v

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.