Python 字串切片

來源:互聯網
上載者:User

標籤:逆序   class   ace   字串   def   字串切片   文法   列印   遍曆   

#-*- coding:utf-8 -*-#字串切片names = "abcdefgh"‘‘‘切片文法names[起始位置:終止位置:步長]起始位置:即字串的下標,可以是正序下標(0,1,2...),也可以是逆序下標(-1,-2,-3...)終止位置:也是字串的下標,但是和起始位置下標不同的是終止位置下標指向的元素並不會被包含在內步長:預設值是1,當步長>0,表示從左向右遍曆,當步長<0,表示從右向左遍曆起始位置或者終止位置的值預設,那麼會根據步長的正負決定遍曆的方向‘‘‘print(names[2:5]) #列印cde   不包含names[5]的值print(names[2:6]) #列印cdef#從e開始截取到字串末尾print(names[4:])  #列印efgh#從e開始截取到倒數第二個字元print(names[4:-1])#間隔著列印字串print(names[::2])  #列印aceg#逆序列印字串print(names[::-1])

 

Python 字串切片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.