Python 字符串索引和切片

Python 字符串索引和切片,python,string,python-2.7,Python,String,Python 2.7,如何查找字符串索引并基于该索引对字符串进行切片 字符串 我的时间1:1000天24小时 我的时间2:3天3小时 我已经编写了以下代码来重设字符串索引 代码 x = line days = x.find("days") hrs = x.find("hours") print x[hours-4,hours] 向我抛出下面的错误 TypeError: string indices must be integers, not tuple 通过手动计算传递整数可以解决此错误,但正如您在时间示例中所看

如何查找字符串索引并基于该索引对字符串进行切片

字符串

我的时间1:1000天24小时
我的时间2:3天3小时

我已经编写了以下代码来重设字符串索引

代码

x = line
days = x.find("days")
hrs = x.find("hours")
print x[hours-4,hours]
向我抛出下面的错误

TypeError: string indices must be integers, not tuple
通过手动计算传递整数可以解决此错误,但正如您在时间示例中所看到的,我无法确定索引是什么,因为它会根据天数和小时数而变化

有谁能建议什么是解决这个问题的好方法吗


Python版本2.7

您需要使用冒号而不是逗号:

x[hours - 4:hours]
糟糕的是,我的语法错误:(离题是
days=x[x.find(“days”)-3:x.find(“days”)]
所谓的pythonic方法?