Sitemap

Accessing string

1 min readSep 2, 2026

--

String is a one of the datatype enclosed in asingle /double quotes.string is immutable we access string using two methods called indexing and slicing.

In Indexing we can access single element/charecter.index starts from 0 .python engine always goes left to right

Ex of Indexing :a=”samanvitha” a[0]/a[5] o/p:s,v

In Slicing we can access sequencec of elements at once.three ways of slicing possitive,negative,mixed slicing.

syntax:vatiable/string[startingindex:endingindex:stepsize]

by default empty starting index takes first position called 0 & ending index takes end of the string.ending index value ignored.

Stepsize means which element/charecter wants to print next 1 or 2 based on stepsize prints. returns empty string if nothing return in output

Possitive Slicing:Python Engine always goes left to right.

Ex of slicing:a=”samanvitha”

a[::] o/p: samanvitha

a[0::]o/p: samanvitha

Get Samanvitha’s stories in your inbox

Join Medium for free to get updates from this writer.

a[1:5:] o/p: aman

a[1:5:1]o/p: aman

a[0:5:2]o/p: aa

Negative Slicing:Python engine goes from right to left .it return reverse of a string.negative slicing starts from -1

ex:a=”samanvitha”

a[-1::-1]o/p:ahtivnamas

a[-5::-1] o/p:vnamas

a[-5:-10:-1] o/p:vnama

Mixed slicing:combination of possitive and negatibe index.

ex: a=”samanvitha”

a[-5:-10:1] o/p: ’’empty string

a[-1:-6:1] o/p:’’empty string

--

--