string index function in python
the operation of this function is very similar to find function.
the just one difference is that if sub string is not found by index function, ValueError exception is raised but find function will return -1.
if you wonder about find function, refer to how to use find function of str in python
in this page, I will introduce index function.
the declaration for index is like below.
the way how to use this function is also almost same.
sub parameter is what you want to find from "str".
and start and end are the range of str. it will be from 0 to the length of string.
the default value of start is 0 and end is the length of string.
let's check it in example.
the just one difference is that if sub string is not found by index function, ValueError exception is raised but find function will return -1.
if you wonder about find function, refer to how to use find function of str in python
in this page, I will introduce index function.
the declaration for index is like below.
str.index(sub[, start[, end]])it is almost same to find function excepting for the function name.
the way how to use this function is also almost same.
sub parameter is what you want to find from "str".
and start and end are the range of str. it will be from 0 to the length of string.
the default value of start is 0 and end is the length of string.
let's check it in example.
1
2
3
4
5
6
7
|
srcString = "hello python"
print(list(enumerate(srcString)))
print("length of string : {}".format(len(srcString)))
print("index of sub : {}".format(srcString.index("python")))
print(srcString.index("python", 0,5))
| cs |
Comments
Post a Comment