新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python字符串如何訪問字符
1、要訪問字符串中的字符,例如最后一個字符,需要知道它在字符串中的位置。

通海網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,通海網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為通海成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的通海做網(wǎng)站的公司定做!
從0到小于字符串長度的1開始編號或索引。
str = 'Antarctica is really cold.'
print('str = ', str)
#first character
print('str[0] = ', str[0])
#last character
print('str[-1] = ', str[-1])
#slicing 2nd to 5th character
print('str[1:5] = ', str[1:5])
#slicing 6th to 2nd last character
print('str[5:-2] = ', str[5:-2])2、如果索引從左到右遵循遞增順序模式,則從右到左遵循遞減順序模式。
即從-1、-2、-3等。因此,如果您想訪問最后一個字符,您可以使用兩種方法。
str = 'Antarctica is really cold.'
a = len(str)
print('length of str ', a)
#last character with the help of length of the string
print('str[a] ', str[a-1])
#last character with the help of indexing
print('str[-1] ',str[-1])以上就是Python字符串訪問字符的方法,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)頁名稱:創(chuàng)新互聯(lián)Python教程:python字符串如何訪問字符
文章起源:http://www.5511xx.com/article/cdegogh.html


咨詢
建站咨詢
