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

Ordinal, index, and slice of Python strings

編輯:Python

Python The sequence number of the string

  • Increasing the sequence number forward
  • Reverse decrement sequence number
  • Index method
  • section
  • Program validation

Increasing the sequence number forward

Positive increasing sequence number is the subscript standard of many languages we are familiar with before , such as C Language and Java Language, etc. .
For example, an array of

Str = "Hello_World"
label 012345678910 Symbol Hello_World

Subscript to be 0 Express ’H’, Subscript to be 10 Express ’d’
Subscript left to right , from 0 Start increasing in sequence

Reverse decrement sequence number

stay Python There is a special labeling method in , It is the serial number of decreasing response
For example, an array of

Str = "Hello_World"
label -11-10-9-8-7-6-5-4-3-2-1 Symbol Hello_World

Subscript to be -11 Express ’H’, Subscript to be -1 Express ’d’
Subscripts from right to left , from -1 Start decreasing in turn

Index method

Indexes can be expressed in two forms

Str="Hello_World"

hypothesis Str = “Hello_World”

The first one is : String with square brackets

“Hello_World”[0] Express Hello_World Of the 0 Elements , That is to say ‘H’

The second kind : Add square brackets to the variable name

Str[0] Express Hello_World Of the 0 Elements , That is to say ‘H’

section

The slice represents the interception of the string
for example Str[0:n] Indicates that the truncated subscript is 0~(n-1) Part of

Program validation

The index code is as follows :

# Verification of positive increasing sequence number and negative decreasing sequence number 
Str="Hello_World"
print(" The original string is :"+Str)
print("\n character string + Square bracket validation ")
print(" Subscript to be 0 The characters of :"+"Hello_World"[0])
print(" Subscript to be -11 The characters of :"+"Hello_World"[-11])
print(" Subscript to be 10 The characters of :"+"Hello_World"[10])
print(" Subscript to be -1 The characters of :"+"Hello_World"[-1])
print("\n Variable name + Square bracket validation ")
print(" Subscript to be 0 The characters of :"+Str[0])
print(" Subscript to be -11 The characters of :"+Str[-11])
print(" Subscript to be 10 The characters of :"+Str[10])
print(" Subscript to be -1 The characters of :"+Str[-1])

Verify success :

The slicing code is as follows

Str="Hello_World"
print(Str[0:6])
print(Str[0:-1])

Output results :


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