Home >Backend Development >Python Tutorial >How to intercept specific parts of a string in python

How to intercept specific parts of a string in python

coldplay.xixi
coldplay.xixiOriginal
2020-12-01 14:04:0345113browse

Python method to intercept a specific part of a string: You can use [str[beginIndex:endPosition]], where str is the string that needs to be intercepted, beginIndex is the subscript of the first character that needs to be intercepted, and endPosition It is the subscript of the last character of the intercepted character.

How to intercept specific parts of a string in python

The operating environment of this tutorial: Windows 7 system, python version 3.9. This method is suitable for all brands of computers.

Related free learning recommendations: python video tutorial

How to intercept specific parts of a string in python :

1. Intercept the string at the specified position

Python string can be understood as an array. To obtain a certain part, you can use str[beginIndex :endPosition], where str is the string that needs to be intercepted, beginIndex is the subscript of the first character that needs to be intercepted, endPosition is the position of the last character to be intercepted, pay attention to the subscript and position (example below), Mark 1 = position; beginIndex and endPosition can be omitted. If not written, the first or last one will be defaulted;

Normal example:

a = "Hello"
 
print "a[1:4] 输出结果:", a[1:4]    #结果 ell
print "a[:4] 输出结果:", a[:4]    #结果 Hell
print "a[1:] 输出结果:", a[1:]    #结果 ello

Of course, beginIndex and endPosition can also be negative numbers. Indicates that the interception direction is from right to left, such as

a = "Hello"<br><br>print a[:-1] #截取从第一个字符开始到倒数第1个字符(不含最后一个) 结果Hell
print a[-3:-1] #截取倒数第三位字符与 倒数第一位之间的字符(注意不包含最后一个字符)结果ll
print a[-3:] #截取倒数第三位到结尾  结果llo

2. Intercept the string according to the specified characters

  • First obtain the subscript position of the character;

  • Then intercept it through the above method;

Python provides the index function to detect whether the string contains a substring, usually expressed as a certain Some specific characters, specific words; a.index(b, begin, end), a is the string that needs to be verified, b is the string, begin is the subscript of the character to start intercepting (default is 0), end is the subscript of the ending character Marker (default is character length)

Example:

str1 = "Hello.python";
str2 = ".";
  
print str1.index(str2);#结果5
print str1.index(str2, 2);#结果5
print str1.index(str2, 10);#结果报错,没找到子字符串

Based on the above, follow the character screenshot example

str1 = "Hello.python";
str2 = ".";
  
print str1.index(str2);#结果5
print str1[:str1.index(str2)]     #获取 "."之前的字符(不包含点)  结果 Hello
print str1[str1.index(str2):] ; #获取 "."之前的字符(包含点) 结果.python

The above is the detailed content of How to intercept specific parts of a string in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn