Index function

I was looking at this code,l thought the index function can only take numbers as arguments from what l read online and here commas are not even used, so l don’t understand this part



Well, the thing to do is to try it yourself at the terminal since it’s quick.
Try to recreate the conditions (type, syntax, etc). Since the line object is a string it helps to just try:


and the like to see the results.

More importantly, there’s two places where you can rely on for the most consistent reference, and that is documentation online, and documentation in the code.

If you type


you can scroll and find the entry for index

 |      S.index(sub[, start[, end]]) -> int
 |      Return the lowest index in S where substring sub is found, 
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      Raises ValueError when the substring is not found.

where if you dissect it, it should start becoming clear how the string method index() works (note how it’s important to distinguish string from other object method).

Finally, you should try typing


and this will settle the final doubt.

(note, I leave the interpretation of the behavior to you since the more useful skill is parsing the documentation, rather than knowing off-hand how a method x works, as that will vary in different versions and languages)

I saw here

When l tried to replicate the code l posted l get value error:

line = "hello people of tuala"

pos = line.index("(")


But what does the error say? It should say something like substring not found. What does that imply?

If you get asked where is the letter x located in apple, how would you reply?

If you get asked where is the letter l in apple what would you say instead?

@jagmeetsond589256300 What is a substring? Can one query for more than just a single char? What happens if you query "apple".index("pl")?

it gives value error substring not found,

X in Apple I would say its not there,

So how can we correct the code above, cause its wrong right

What code is wrong? The book code? The book code works as far as I see it.

@jagmeetsond589256300 another thing you might want to consider if you’re using code from a book (or the internet) to practice is, what version python is it using?
A lot of technical errors arise from mismatched versions. There’s a lot of great python 2 material that simply will not work on python 3. You can use virtual environments to run those, however.

I just tried to replicate the book code and i got value error, personally am using python 3.9, not sure what the book is using

The important thing is to notice why i gives a value error. You get a value error whenever you can’t find the substring that’s within the string.

So if you’re looking for a parenthesis that’s not in a piece of text, you will get an error. Depending on what you want to do, this is not an issue as you can program around this issue.

ok l get it they are looking for the lowest index of " and the substring starts at ( and ends "

The index method returns the index of the first occurrence of a substring in a string. If the substring is not present in the string, then the ValueError: substring not found exception is thrown. In this situation, you can either choose to handle the error using ttry-except clause, or use str.find() instead which returns an integer value between -1 and len(input_number), or do a set membership test on the original string.