0
Follow
0
View

python Basics - Arrays

du44022822 注册会员
2023-02-27 12:35

the reference answer ChatGPT < br / > ٩(๑ ^ o ^ ๑) ۶

In this code we use slicing, where we get parts of the string by specifying the index range.

Here, b[-5:-2] means a substring from the fifth character from the bottom(including this character) to the second character from the bottom(excluding this character). Therefore, the "orl" in the string "World" is this substring.

Note that in Python, strings can be indexed either positively or negatively. The positive index counts from the beginning of the string, with the index of the first character being 0, in ascending order. Negative indexes count from the end of the string, and the index of the first character from the bottom is -1, decreasing in order. Thus, b[-5:-2] actually represents a substring from the fifth to the third character from the bottom.

kcsj999 注册会员
2023-02-27 12:35

-5:-2, starting with index -5 and ending with index -2, slicing, ending with index not included in slice

dy15145071020 注册会员
2023-02-27 12:35

The starting and ending coordinates of the slice are negative, indicating that the value is inverted, that is, the calculation starts from the right side. The corresponding relationship between specific data and index is as follows:
b: Hello, W o r l d !
coordinate: -5-4-3-2-1
slice value is left closed and right open, that is, does not contain the element in the position of -2, so the output is orl.

If it helps, please click to accept ~

deathguy000 注册会员
2023-02-27 12:35

There is an error in the code you are printing. print should be followed by the content you want to print instead of the variable b. The correct way to write it is print(b). The print function prints the contents of parentheses to the screen, not the contents of parentheses back, so there is no or output.
The answer is not easy, so take it.

divavitas 注册会员
2023-02-27 12:35
1 2 3 4 5 6 7 8 9 10 11 12 -6 -5 -4 -3 -2 -1 .
< !- - - - - >

About the Author

Question Info

Publish Time
2023-02-27 12:35
Update Time
2023-02-27 12:35

Related Question