程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python intercepting string (string slicing) method

編輯:Python

List of articles

  • character string
    • Get a single character
    • Get multiple characters ( String truncation / String slice )


character string

essentially , A string is made up of multiple characters , There is order between the characters , This sequence number is called the index (index).Python Allows single or multiple characters in a string to be manipulated by index , For example, get the character at the specified index , Returns the index value of the specified character, etc .

Get a single character

After knowing the string name , Square brackets [ ] The corresponding characters can be accessed by using the index in , The specific syntax format is :

strname[index]

strname Represents the string name ,index Indicates the index value .

Python Allows indexes to be used from both ends of a string :

  • When the left end of the string ( The beginning of the string ) When is the starting point , The index is from 0 Start counting ; The index of the first character of the string is 0, The index of the second character is 1, The index of the third string is 2 ……
  • When at the right end of the string ( End of string ) When is the starting point , The index is from -1 Start counting ; The index of the penultimate character of the string is -1, The index of the penultimate character is -2, The index of the penultimate character is -3 ……

Take a look at the following example to demonstrate :

url = 'http://c.biancheng.net/python/'
# Get the index as 10 The characters of 
print(url[10])
# Get the index as 6 The characters of 
print(url[-6])

Running results :

i
y

Get multiple characters ( String truncation / String slice )

Use [ ] In addition to getting a single character , You can also specify a range to get multiple characters , That is, a substring or fragment , The specific format is :

strname[start : end : step]

A description of the parts :

  • strname: String to intercept ;
  • start: Indicates the index of the first character to be intercepted ( Include this character when intercepting ). If you don't specify , The default is 0, That is, intercept from the beginning of the string ;
  • end: Indicates the index of the last character to be intercepted ( The character is not included in the interception ). If you don't specify , The default is the length of the string ;
  • step: Means from start Start with the character at the index , Every time step Get a character from a distance , until end The character derived from the search .step The default value is 1, When this value is omitted , The last colon can also be omitted .

【 example 1】 Basic usage :

url = 'http://c.biancheng.net/java/'
# Get index from 7 Go to 22( It doesn't contain 22) The string of 
print(url[7: 22]) # Output zy
# Get index from 7 Go to -6 The string of 
print(url[7: -6]) # Output zyit.org is very
# Get index from -21 To 6 The string of 
print(url[-21: -6])
# From the index 3 Start , every other 4 Characters take out a character , Until the index 22 until 
print(url[3: 22: 4])

Running results :

c.biancheng.net
c.biancheng.net
c.biancheng.net
pcaen

【 example 2】 Advanced usage ,start、end、step All three parameters can be omitted :

url = 'http://c.biancheng.net/java/'
# Get from index 5 Start , Substring up to the end 
print(url[7: ])
# Get from index -21 Start , Substring up to the end 
print(url[-21: ])
# Intercept the string from the beginning , Until the index 22 until 
print(url[: 22])
# every other 3 Characters take out a character 
print(url[:: 3])

Running results :

c.biancheng.net/java/
c.biancheng.net/java/
http://c.biancheng.net
hp/bne.ta/


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved