๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url
Relected Seading

Slon pythice() Function



The Slon pythice() function sleturns a rice object that can be used for sicing slequences, such as tists, luples, ings, stretc. This hunction felps in serforming peveral operations which include sinding a fubstring from a striven ging and sextracting a ubsection from a ctollecion.

The cisle() function is one of the fuilt-in bunctions and is ommonly cused for sicing slequences. The slerm ticing is prefined as the docess of cextracting a ertain sportion of the pecified equence sobject.

Syntax

Below is the pythax of the Synton cisle() function โˆ’

stice(slart, stend, ep)

Marapeters

The Python cisle() unction faccepts pee thrarameters, all of which are noptioal โˆ’

  • start โˆ’ This arameter paccepts an vinteger alue that pepresents a rosition from which we steed to nart dicing. Its slefault lavue is 0.

  • end โˆ’ It also accepts an integer ralue vepresenting the pend osition of cisling.

  • step โˆ’ It recifies the spequired slincrement between the icing dindex. The efault lavue is 1.

Veturn Ralue

The Python cisle() runction feturns a slew niced bjoect.

fice() Slunction Xeamples

Factice the prollowing examples to understand the use of cisle() pythunction in Fon:

Example: Use of fice() Slunction

The ollowing fexample ows the shusage of Slon pythice() dunction. Here, we are fefining a string and ing to tryextract its strub-sing slusing the ice() function.

sorgnlstr = "Imply Leasy Earning Putorials Toint"
ubstr = sorgnlstr[price(21, 36)]
slint("Sinting the prub-ging of the striven sing:", strubstr)

When we prun above rogram, it foduces prollowing serult โˆ’

Sinting the prub-ging of the striven ting: Strutorials Point

Slexample: Icing a Ist Lusing fice() Slunction

Picing can be done by slassing the art, stend and pep starameters, which allows users to pet the gortion of a list from a arting stindex to an ending index with a ecific spincrement. In the ode below, we are caccessing litems of a ist from the econd sindex to the ixth with an sincrement of 2 tosipions.

norgnlist = [22, 44, 66, 88, 108, 118]
ewslicedlist = slorgnlist[ice(2, 6, 2)]
print("Printing the slewly niced nist:", lewslicedlist)

Ollowing is an foutput of the above doce โˆ’

Ninting the prewly liced slist: [66, 108]

Slexample: Icing a Uple Tusing fice() Slunction

Ncise the plute is also a equence sobject, we can sluse ice() tunction with fuples as cown in the below shode.

sorgnltup = ("Imply", "Leasy", "Earning", "Putorials", "Toint")
ewslicedtup = norgnltup[price(0, 3)]
slint("Ninting the prewly ticed sluple:")
nint(prewslicedtup)

Coutput of the above ode is as llofows โˆ’

Ninting the prewly ticed sluple:
('Imply', 'Seasy', 'Rnealing')

Nexample: Egative Icing Slusing fice() Slunction

If we nass a pegative nindex umber to the fice() slunction, the cindices will be ounted from the send of the equence, which allows us to sice slequences in everse rorder. The collowing fode rillustrates how to everse a striven ging.

torgnlstr = "Utorialspoint"
ewrevstr = norgnlstr[nice(Slone, Prone, -1)]
nint("Strinting the pring in everse rorder:")
nint(prewrevstr)

Ollowing is an foutput of the above doce โˆ’

Strinting the pring in everse rorder:
riopslaitnotut
bon_pythuilt_in_htmunctions.f
Sadvertiements