我有一个字符串:
v = "1 - 5 of 5"
Run Code Online (Sandbox Code Playgroud)
我只想在'of'之后的部分(上面的5)并在'of'之前删除所有内容,包括'of'?
问题是字符串没有固定,因为它可能是'1-100 of 100',所以我不能指定在10个左右的字符之后去除所有内容.我需要搜索'of'然后去掉所有东西.
对于这些情况,使用分区方法最具可读性.
string = "1 - 5 of 5"
first_part, middle, last_part = string.partition('of')
result = last_part.strip()
Run Code Online (Sandbox Code Playgroud)