0

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

2
  • String literals in Python don't have any "font"; if two characters look different, it's because they're different characters. You can replace the fancy apostrophes with normal ones like this: txt = txt.replace("’", "'") Commented Apr 16, 2019 at 15:19
  • As already noted these are different characters. If you encounter situation like this one you might use == operator to check if characters are same - in this case print("’"=="'") outputs False. Commented Apr 16, 2019 at 15:28

2 Answers 2

3

It is because and ' are different characters

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Thanks @Devesh for responding. But what if the text available is huge. and an individual doesn't know if the apostrophe present is: " ’ " or " ' "
Just run a text.replace('’',"'")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.