Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to find the location of substrings

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use python to find the location of the substring", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to find the location of the substring with python.

Sample code

String = "abcdcdefg"

Print string.index ('cde')

Use the index method to implement a method to find the location of the substring. Look at the following two strings:

String = "abcdcdefg"

Sub = "cde"

By observing, you will find that the string sub exists in string, and the starting position is 4, which is the result we want to get.

Logical analysis

The first character of sub is c. I should first find a character c in string, which may or may not be the beginning of the substring cde. One paragraph in string is cdc, the first two characters are indeed the same, but the third character is different, but it doesn't matter. Let's find the c character in string first.

After finding the character c in string, you should then compare the rest. If the rest corresponds to each other, you will find the starting position of the substring.

Sample code

# coding=utf-8

String = "abcdcdefg"

Sub = "cde"

I = 0

# pay attention to this assignment method. It is simple and does not need to write multiple lines.

String_len,sub_len = len (string), len (sub)

# record the starting position of the substring

Index =-1

# loops are everywhere

While I < string_len:

# be sure to find a character equal to sub [0] first

If string [I] = = sub [0]:

Index,m = iPersoni

J = 0

# next to compare the rest

While j < sub_len and m < string_len\

And string [m] = = sub [j]:

M + = 1

J + = 1

# the most subtle part is this code

If not j = = sub_len:

Index =-1

Else:

Break

I + = 1

Print index

Program analysis

String_len,sub_len = len (string), len (sub) is a very convenient way to assign variables. If you don't write this, you have to divide it into two statements.

For while loops, unlike for loops, you have to firmly control the conditions of the loop, that is, the expressions followed by while, which determine when the loop ends.

Before the start of the second while loop, I created a variable named m instead of continuing to use I as the index for comparison, because I was used to control the outer while loop. If I executed I + = 1 in the second loop, it would affect the first while loop. Don't forget that the first while loop is used to find the character position equal to sub [0], if I is added 1 for no reason. Then there must be a character in the first while loop that is not traversed to the

The most subtle part of the program is if not j = = sub_len: how do I know that the substring matches at the end of the second while loop? If the value of j is equal to sub_len, it means that the substring is matched, because at the end of the loop, j equals sub_len, which means that every character of sub is traversed and corresponds to a part of string one by one, otherwise, the loop will be interrupted because it does not meet the conditions. I asked index to equal I before the start of the second while loop, so that I recorded the possible start position of the substring. If j is indeed equal to sub_len, then index is the starting position of the substring. If j is not equal to sub_len, the value of index is not the starting position of the substring, so re-assign it to-1.

Break will only be executed when the start of the substring is found, because I don't want to keep looking, even if there is a substring

At this point, I believe you have a deeper understanding of "how to use python to find the location of the substring". 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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 265

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report