In Pythonfind()
Functions are methods of string objects that can only be called on strings. They are used to detect the target character (string) in the query string and return the index. If there is a target character (string), it returns the index of the first occurrence of the target character (string) (the character index in the string is calculated from left to right, and is calculated from 0); if there is no target character (string), it returns -1.
Syntax: (string, start, end)
str: The string to be queried.
string: The target character (string) to be detected, which can be a single character or a string composed of multiple characters.
start: The starting position of searching from the string to be queried can not be filled in, but cannot be empty, and it is 0 if not filled in.
end: The end position of searching from the string to be queried. If there is an end, you must have a start, otherwise an error will be reported; it can be not filled in, and the length of the string is not filled in.
Example
Example 1. Simple character search
1. Find the target character in the string to be queryed and return the index of the target character.
s="Hello Word!" a="o" print("The index of the character o in the string is:",(a))#The output result is a characteroThe index in the string is: 4
2. Find the fixed position target character of the string to be queryed and return the index of the target character.
s="Hello Word!" a="o" print("The index of the character o in the string is:",(a,0,4))#The output result is that the index of character o in the string is: -1print("The index of the character o in the string is:",(a,6,8))#The output result is the index of character o in the string: 7print("The index of the character o in the string is:",(a,1))#The output result is the index of character o in the string: 4
3. Find all the characters (strings) that meet the conditions in the string to be queryed and return the string index.
s="Hello Word!"#Define stringstart_index=0#Initialize the start positionwhile True: index=("o",start_index) if index==-1: break else: print("The index position of character o is:",index) start_index=index+1#Update the starting position''' The output result is: The index position of character o is: 4 The index position of character o is: 7 '''
4. Fill incorrectly in search of conditions
s="Hello Word!" a="o" print("The index of the character o in the string is:",(a,,10))#The start position is empty and the data is not filled in, resulting in an error in the run ''' File "e:", line 3 print("The index of the character o in the string is:",(a,,10))#The output result is that the index of character o in the string is: -1 ^ SyntaxError: invalid syntax '''
Example 2: Practice with if statements
7 games
A circle of people starts to count the number from 1. Whenever a multiple or number of 7 is included, you cannot shout specific numbers. You can skip them in the form of applause. Screaming a number or skipping it without applause is considered a violation and you need to accept punishment.
Simplify it, enter an integer, calculate all the numbers that meet the criteria in this number and count the number.
n=int(input("Please enter the number:"))The result type returned by #input function is strnum=1 sum=0 while num <= n: if num % 7==0 or str(num).find("7")!=-1: print(str(num) + " pass") sum+=1 else: pass num+=1 print("sum =",sum) print("#"*20) ''' Enter the number 30 and the result is: Please enter the number: 30 7 pass 14 pass 17 pass 21 pass 27 pass 28 pass sum = 6 ##################################### '''
Example 3: Practice with for statements
Flawed product screening
There are a batch of products that have been classified, with the number 100-109 in the product list, which is divided into three levels: excellent, good and inferior. The order is: excellent, good and inferior, good and inferior, inferior, inferior, inferior, inferior, inferior, inferior, inferior, inferior, inferior, inferior, inferior, inferior, inferior. Please select the product numbers of inferior products.
#Definition DictionaryProducts={100:"Odd",101:"Good",102:"Good",103:"Unfortunate",104:"Inferior",105:"Excellent",106:"Excellent",107:"Understanding",108:"Good",109:"Understanding"} values=()#Take out all valuesfull_list=[]#Create an empty list # Take out all the matching values, add them to the list, and deduplicate themfor value in list(values): if ("inferior")!=-1: full_list.append(value) set_list=set(full_list) #Find all products and numbers that meet the criteriafor x in range(100,110): for y in set_list: if (x)==y: print("Product %s is inferior product, inferior product number is %s"%(y,x)) ''' The output content is: The inferior product is inferior product, the inferior product number is 103 The inferior product is inferior product, the inferior product number is 104 The inferior product is inferior, the inferior product number is 107 The inferior product is inferior, the inferior product number is 109 '''
This is the end of this article about the usage of find() in Python. For more information about the usage of find() in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!