A function that determines which index a particular character appears in a string

Asked 2 years ago, Updated 2 years ago, 39 views

Where is the function to find out which index a particular character appears in a string?

For example,

mystring = "hello"

myindex = mystring.h #myindex = 0
I'm looking to

this function.

string python

2022-09-21 15:38

1 Answers

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")


2022-09-21 15:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.