Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I'm pretty new to Python and am completely confused by .join() which I have read is the preferred method for concatenating strings.

I tried:

strid = repr(595) print array.array('c', random.sample(string.ascii_letters, 20 - len(strid))) .tostring().join(strid)

and got something like:


Why does it work like this? Shouldn't the 595 just be automatically appended?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code for appending a string, just concatenate it with the + sign.

An example is as follows:-

>>> a = "Hello, " 

>>> b = "world" 

>>> str = a + b 

>>> print 

str Hello, world

join connects strings together with a separator. The separator is what you place right before the join


>>> "-".join([a,b]) 

'Hello, -world'

What join does is it takes a list of strings as a parameter.

To know more about this you can have a look at the following video tutorial:-

Browse Categories