Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (4k 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:

5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5

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

1 Answer

0 votes
by (8.7k points)

In python we have a string method join(), which usually takes a list and joins it with the string:

For Example:

“/”.join([‘1’,’2’,’3’])

So the output will be 
1/2/3 

In the following code, it is better to use “+”

 print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))

.tostring() + strid

 

Related questions

0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 7, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.6k points)

Browse Categories

...