In Python to talk about, how to intercept part of the character string

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/deniro_li/article/details/102765060

String in Python like a list, you can also use the subscript is sliced ​​to obtain a substring we need.

str='近日,美国宇航局公布了一张在好奇号在火星上拍摄的“自拍照”。'
print(str[0])
print(str[3])
print(str[-1])
print(str[0:3])
print(str[3:])

operation result:

Near
the United States
.
Recently,
NASA announced in a Curiosity on Mars taken "from taking pictures."

  • -1 subscript, it represents the number of back to front, first.
  • If the next two into the reference standard to calibrate a range, then the index will contain the beginning, but not the mark at the end.

Note: The string sections will not modify the original string, so we have to get a slice of the string, into another variable.

It is not that simple ah O (∩_∩) O haha ​​~

Guess you like

Origin blog.csdn.net/deniro_li/article/details/102765060