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"
Subscript to be 0 Express ’H’, Subscript to be 10 Express ’d’
Subscript left to right , from 0 Start increasing in sequence
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"
Subscript to be -11 Express ’H’, Subscript to be -1 Express ’d’
Subscripts from right to left , from -1 Start decreasing in turn
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’
The slice represents the interception of the string
for example Str[0:n] Indicates that the truncated subscript is 0~(n-1) Part of
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 :