You can use two methods: find and index
The simple way to write is
Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.
Returns the index where sub appears for the first time in s[start:end]. Returns -1 if sub is not found.
mystring = "hello python world!"
myindex = mystring.find("l")
Like find(), but raise ValueError when the substring is not found.
Similar to find(). If sub is not found, a ValueError occurs.
mystring = "hello python world!"
myindex = mystring.index("l")
© 2025 OneMinuteCode. All rights reserved.