Skip to main content

Posts

Showing posts with the label python3

Python3 List index Method

Description The method index() returns the lowest index in list that obj appears. Syntax Following is the syntax for index() method − list.index(obj) Parameters obj − This is the object to be find out. Return Value This method returns index of the found object otherwise raise an exception indicating that value does not find. Example: Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> testList = ['abc','foo','bar','woo','adc'] >>> print("Index of 'foo' is:%d"%testList.index('foo')) Index of 'foo' is:1 >>> print("Index for 'bar' is:%d"%testList.index('bar')) Index for 'bar' is:2 >>>

python3 string find method

Description It determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given. Syntax str.find(str, beg=0, end=len(string)) Parameters str − This specifies the string to be searched. beg − This is the starting index, by default its 0. end − This is the ending index, by default its equal to the length of the string. Return Value Index if found and -1 otherwise. Example: Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> str1 = "This is a string to test the find method." >>> str2 = "string" >>> print(str1.find(str2)) 10 >>> print(str1.find(str2,10)) 10 >>> print(str1.find(str2,10+1)) -1 >>>