Created
April 11, 2022 21:09
-
-
Save aaronshaver/7593f2afb007eadc0a23a4771036799a to your computer and use it in GitHub Desktop.
find() versus index() in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# find() doesn't return an exception, and is perhaps better suited for cases | |
# where we don't know if the substring exists; it returns -1 if not found | |
# index() will throw a ValueError exception if the value being searched for doesn't exist | |
class Solution: | |
def reversePrefix(self, word: str, ch: str) -> str: | |
return word[:word.find(ch) + 1][::-1] + word[word.find(ch) + 1:] | |
# this problem says to reverse the part of the string from 0 to first occurence of character | |
# then return that reversed output with the rest of the string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment