Adarsh Namdev
Adarsh Namdev

Reputation: 127

How to fix this error when using the str.index() method?

I am having a text string- "The class also contains a string for the collection’s title and a scalar Boolean variable indicating whether the collection is currently editable."

Here, I'm trying to fetch the index of the sub-string "collection's" but getting the below error. ValueError: substring not found

I understand that the apostrophe in the sub-string in the actual text is in different font. So, is there any way where we can bypass difference in the font.

txt = "The class also contains a string for the collection’s title and a scalar Boolean variable indicating whether the collection is currently editable."

print(txt.index("collection's"))

Expected is to have the index of the sub-string- "collection's"

actual result is: ValueError: substring not foundenter code here

Upvotes: 0

Views: 68

Answers (2)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

Just make sure your text and the string you are looking for use the same form of apostrophe '

txt = "The class also contains a string for the collection's title and a scalar" \
   "Boolean variable indicating whether the collection is currently editable."
print(txt.index("collection's"))
#41

Upvotes: 0

user3469811
user3469811

Reputation: 678

It is because and ' are different characters

Upvotes: 3

Related Questions