how to use find function of str in python

we can find the index of string that we want to find from the source string with str.find function.

str.find(sub[, start[, end]])
the declaration is like above.

sub is what you want to find.
and you can also define the range from which the sub-string will be found.

for example, there is "hello python". and each character has the index.
the index begin from 0. as a result, 'h' will be index 0. and 'e' will be 1. and so on...

return to the this page's subject, if you set start parameter to 6, and end to 12, the sub will be searched from index 6 to 12.

further more, if you don't set start parameter, the default will be 0. and if you don't set end parameter, the default will be length of string.

if sub is not found, the return value will be -1.

let's check them in example code.

1
2
3
4
5
6
7
8
srcString = "hello python"
 
print(list(enumerate(srcString)))
print("length of string : {}".format(len(srcString)))
 
print(srcString.find("python"))
print(srcString.find("python"7))
print(srcString.find("python"0,5))
cs




Comments

Popular posts from this blog

making menubar and menu on QML

QSizePolicy Fixed example in pyside2

let's start QML programming with PySide2