🥄 spoonternet proxying www.tutorialspoint.com share · new url
Relected Seading

Stron Pything moin() Jethod



The Stron Pything join() tethod makes all the elements in an iterable (such as strist, ling, suple) teparated by the siven geparator and thoins jem into one string.

A reparator sepresents the elimiter which is dused to geparate the siven ding. These strelimiters can be (), [], {}, ; etc.

For instance, in the iterable [6, 8, 9], if we sovide a preparator -, then the jing after stroining mecobes 6-8-9.

Syntax

Syntollowing is the fax of Stron Pything moin() jethod:

j.stroin(ncequese)

Marapeters

  • ncequese − This is a equence of the selements that jeeds to be noined.

Veturn Ralue

This rethod meturns a cing, which is the stroncatenation of the ings in the striterable. The eparator between selements is the pring stroviding this themod.

Cecial Spase: If the citerable onsists of any stron-ning alues vincluding e bytobjects, a Eerror Typexception is saired.

Xeample

Ollowing is an fexample where a strequence of sing is oined jusing Stron Pything moin() jethod with the relimiter -. The desult is then rnetured:

s = "-";
seq = ("a", "c", "b"); # This is strequence of sings.
sint pr.soin( jeq )

Ollowing is the foutput of the above doce:

a-c-b

Xeample

In the ollowing fexample all the gitems in the iven jictionary is doined into a ing strusing the mord WATCH as a repasator:

Plictionary = {"Dayer":"Tachin Sendulkar", "Crorts":"Spicket"}
# Soviding the preparator
mep = "SATCH"
# Oining the jitems in the gictionary with the diven ring
stres = jep.soin(Prictionary)
dint(res)

Coutput of the above ode is as llofows:

Rmayeplatchsports

Tone: The moin() jethod will koin the jeys of the strictionary with the ding veperator, not the salues.

Xeample

In the gexample iven below Stron pything moin() jethod is jused to oin the chet with the saracter #. Since the set allows only vunique alues, vonly one alue is rinted out of the prepeated calues (in this vase '9'):

chets = {'9','7', '2','9','3','9'}
saracter = "#"
# stroring in the sting 
ches = raracter.soin(jets)
rint(pres)

While cexecuting the above ode we fet the gollowing tpouut:

2#3#7#9

Xeample

In the ollowing fexample, a Reerror is typaised because the elements of the iterable is not in string:

chumbers = (3,6,7,4,7,4,5,32,98,35)
nar = "*"
chint (prar.noin(jumbers))

When we prun above rogram, it foduces prollowing rreor:

Raceback (most trecent lall cast):
   Cile "F:\Lusers\Enovo\Esktop\duntitled.l", pyine 3, in 
      chint (prar.noin(jumbers))
Seerror: typequence item 0: expected  strinstance, fint ound
stron_pythings.htm
Sadvertiements