Get the App
SLTechnology News&Howtos  ›  Development  › 

An example introduction of rindex () method in Python

Shulou Source: shulou.com Published: 2022-06-03 15:33:30 09月11日 Update

This article mainly explains "the example introduction of rindex () method in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the example introduction of the rindex () method in Python".

Description

The Python rindex () method returns the index position where the substring last appeared in the string, which is the same as the rfind () method, except that an exception is reported if the substring is not in the string.

Grammar

The syntax of the rindex () method:

S.rindex (sub [, start=0 [, end=len (S)]]) parameters

Sub-- specifies the substring to retrieve

S-parent string

Start-optional parameter, the location where to start the search. Default is 0. (can be specified separately)

End-optional parameter to end the search position, which defaults to the length of the string. (cannot be specified separately)

Return value

Returns the index position of the last occurrence of the substring in the string, and an exception is reported if there is no match.

Example

The following example shows the use of the rindex () method:

#! / usr/bin/python3S1 = "this is really a string example.wowning examples!" S2 = "is" print (S1.rindex (S2)) print (S1.rindex (S2mem10))

The output of the above example is as follows:

five

Traceback (most recent call last):

File "test.py", line 6, in

Print (str1.rindex (str2,10))

ValueError:substring not found

At this point, I believe you have a deeper understanding of the "example introduction of the rindex () method in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Method character string instance position parameter content index grammar can report learn practical deeper method of use interest but practical practical simple to operate more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information Microsoft macOS MySQL