0

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

clinomaniac
  • 2,200
  • 2
  • 17
  • 22
mross01
  • 1
  • 1
  • 3
    Do you know what `text[0, int(stringloc)]` is doing? Maybe you wanted `text[:int(stringloc)]`? – cs95 Mar 22 '18 at 23:34
  • I want it to start at first character and search till http is in the string. I have a string. I want to search for a substring, but only in the string up to the http – mross01 Mar 22 '18 at 23:36
  • 1
    So, you wanted `text[:int(stringloc)]`. – cs95 Mar 22 '18 at 23:37
  • sorry not sure how that got duplicated – mross01 Mar 22 '18 at 23:37
  • I guess I am not sure exactly what your line of code does. Very new to python – mross01 Mar 22 '18 at 23:39
  • Try looking at a tutorial or the documentation before asking a question, then. Some good sources are here: https://sopython.com/wiki/What_tutorial_should_I_read%3F – cs95 Mar 22 '18 at 23:39
  • Well that worked. Thank you. Can you tell me what that line of code means. I see what it does but not sure what the ":' does and I am assuming the find command defaults to 0 if only one arg is provided – mross01 Mar 22 '18 at 23:41
  • https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation – cs95 Mar 22 '18 at 23:42
  • I searched for hours before posting – mross01 Mar 22 '18 at 23:42
  • Like I said, learn the language before you start programming with it... – cs95 Mar 22 '18 at 23:42

1 Answers1

0

You may wanted use index like that [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 

In [5]:

Roushan
  • 4,074
  • 3
  • 21
  • 38