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

How to use strings in Python 2-2

編輯:Python

stay Python The index can be used in the string of .

1 General use of indexes

Using indexes in strings , Similar to using indexes in arrays , first “ Elements ” The index value of is 0, After that, add , Pictured 1 Shown .

chart 1 General use of indexes

From the picture 1 It can be seen that , Variable word The type of is string ,word[0] Represents the first element in the string , and word[5] Represents the sixth element in the string . As you can see from the output , stay Python There is no “ character ” type , It's a single “ character ” It's also a string , There is only one in the string “ character ”.

2 Special use of indexes

2.1 When the index is negative

stay Python in , The value of the index can be negative , among -1 Represents the last element ,-2 That's the penultimate element , And so on , Pictured 2 Shown .

chart 2 When the index is negative

 2.2 The index is an interval

2.2.1 Specify the interval between the left and right endpoints

stay Python in , The value of an index can be an interval , This is called slicing (slicing), Pictured 3 Shown . It should be noted that , This interval is “ Left closed right away ” Of , That is, include the value on the left and not the value on the right .

chart 3 The index is a range

among ,word[0:2] Represents from the first element to the third element , Include the first element but not the third ;word[2:5] Means from the third element to the fifth element , Include the third element but not the fifth .

2.2.2 The interval of the default endpoint

The endpoint of the index interval can use the default value , The default value for the left endpoint is 0, The default value for the right endpoint is the string length , Pictured 4 Shown .

chart 4 The interval of the default endpoint

  among ,word[:2] The left default endpoint is used , Equivalent to word[0:2];word[1:] The right default endpoint... Is used , Equivalent to word[1:6];word[:] The default endpoints at both ends are used , Equivalent to word[0:6].


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