Find and string getting - string indices must be integers

时间:2018-03-22 23:33:36

标签: python

Can someone tell me what is wrong with this code. I keep getting string indices must be integers and I have tried everything I can think of. Its actually in an if statement but I stripped it down to this to see if i could find the problem. The Find is returning 23 for the string I am searching. I added the int function to see if that would fix it.

stringloc = 1
stringloc = text.find('http')
print(stringloc)
print(text[0, int(stringloc)])

Here is the return message print(text[0, int(stringloc)]) TypeError: string indices must be integers

1 个答案:

答案 0 :(得分:0)

您可能希望使用类似[start:end]

的索引
In [1]: text="this is the text for demo"

In [2]: stringloc=text.find('text')

In [3]: stringloc
Out[3]: 12

In [4]: print(text[0:stringloc])
this is the 

在[5]中: