⊗pyPmBsSSy 24 of 208 menu

String characters in Python

We can access any character in a string using its index (sequence number), enclosed in square brackets:

txt = 'abcde' print(txt[1]) # 'b'

Note that character numbering starts at zero.

You can also find a character from the end of the line. To do this, write its number with a minus sign. In this case, the last character will start with index -1:

txt = 'abcde' print(txt[-1]) # 'e'

Given a string:

txt = 'abcdef'

Print the first character of this string to the console.

Given a string:

txt = 'abcdef'

Print the second character of this string to the console.

Given a string:

txt = 'abcdef'

Print the last character of this line to the console.

Given a string:

txt = 'abcdef'

Print the second to last character of this line to the console.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline