join() function in python by R4R Team

- join() is a python in-build function which convert the given sequence into the string.this sequence may be list, tuple , set and range.
syntax is ''.join(seq)

Example

list is ["one","two","third"]
string we want is "one-two-third"


program-

#join on list
l=["one","two","three"]
s='-'.join(l)
print(s)
s=''.join(l)
print(s)
s=','.join(l)
print(s)
s='/'.join(l)
print(s)

#join on set
s=set(['1','2','3'])
ans='-'.join(s)
print(ans)
ans=''.join(s)
print(ans)
ans=','.join(s)
print(ans)
ans='/'.join(s)
print(ans)


output-

one-two-three
onetwothree
one,two,three
one/two/three
2-3-1
231
2,3,1
2/3/1


-In this program, we convert the list or set into the string by using ''.join(seq) and print them.
- here seq must contain string or alphabet, if sequence contain integer or float value then it will generate the error like
TypeError: sequence item 0: expected str instance, int found




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!