Python exercises the Find function that implements a string by itself

Source: Internet
Author: User
# Fifth: The find function that implements a string by itself
# 1. Find another string in a string
# 2. Found where the first occurrence of the return occurred
# 3. Not found return-1
# 4. Parameter S1 is the source string, parameter s2 is the string to find
def index_of_str (S1, S2):
#split这个 function to split the character and return a list of the split elements
Lt=s1.split (S2)
#判断分割后的列表的元素个数, if the number of elements is one, there is only one element in the list stating that there are no S2 characters in S1
If Len (LT) ==1:
Return-1
#多于一个元素, the proof is divided. Using the index function to find the subscript of the S2 element in the list
Else
return S1.index (S2)
s1= "12345678"
S2= "45"
Index_of_str (S1,S2)
Print (Index_of_str (S1,S2))

#知识点梳理, the main practice of Split (), index (), the use of attribute functions, built-in function len () proficiency level

Python exercises the Find function that implements a string by itself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.