222

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
  • 1
    I think you tapped your Yubikey while the program was running
    – Trenton
    Commented Jun 14, 2017 at 17:54

10 Answers 10

318

Look carefully at your output:

5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5
^                 ^                 ^

I've highlighted the "5", "9", "5" of your original string. The Python join() method is a string method, and takes a list of things to join with the string. A simpler example might help explain:

>>> ",".join(["a", "b", "c"])
'a,b,c'

The "," is inserted between each element of the given list. In your case, your "list" is the string representation "595", which is treated as the list ["5", "9", "5"].

It appears that you're looking for + instead:

print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring() + strid
4
  • 1
    Why is it not: 5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5wlfgALGbXOahekxSs ? With the string appended to the last element? Commented Dec 9, 2009 at 19:27
  • 12
    One reason is this gives join the useful property of being the inverse of split (docs.python.org/library/stdtypes.html#str.split)
    – cobbal
    Commented Dec 9, 2009 at 19:31
  • 4
    If you want another delimiter, put an empty string at the end of your list. ','.join(['a', 'b', 'c', '']) gives "a,b,c,"
    – tgray
    Commented Dec 9, 2009 at 19:31
  • 9
    OP was probably confusing string.join with os.path.join which indeed concatenates paths
    – Juan Campa
    Commented Jan 28, 2014 at 17:33
97

join takes an iterable thing as an argument. Usually it's a list. The problem in your case is that a string is itself iterable, giving out each character in turn. Your code breaks down to this:

"wlfgALGbXOahekxSs".join("595")

which acts the same as this:

"wlfgALGbXOahekxSs".join(["5", "9", "5"])

and so produces your string:

"5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5"

Strings as iterables is one of the most confusing beginning issues with Python.

1
  • 9
    upvote for pointing out what might be the crux of the confusion: strings are iterable so they act like lists of chars. Commented Sep 30, 2014 at 5:44
61

To append a string, just concatenate it with the + sign.

E.g.

>>> 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. E.g.

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

Join takes a list of strings as a parameter.

1
  • 5
    This kind of direct and concise answer should be advocated by our community. @Dan
    – DehengYe
    Commented Aug 11, 2015 at 10:45
8

join() is for concatenating all list elements. For concatenating just two strings "+" would make more sense:

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

To expand a bit more on what others are saying, if you wanted to use join to simply concatenate your two strings, you would do this:

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

There is a good explanation of why it is costly to use + for concatenating a large number of strings here

Plus operator is perfectly fine solution to concatenate two Python strings. But if you keep adding more than two strings (n > 25) , you might want to think something else.

''.join([a, b, c]) trick is a performance optimization.

0

On providing this as input ,

li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
s = ";".join(li)
print(s)

Python returns this as output :

'server=mpilgrim;uid=sa;database=master;pwd=secret'
0
list = ["my", "name", "is", "kourosh"]   
" ".join(list)

If this is an input, using the JOIN method, we can add the distance between the words and also convert the list to the string.

This is Python output

'my name is kourosh'
0

"".join may be used to copy the string in a list to a variable

>>> myList = list("Hello World")
>>> myString = "".join(myList)
>>> print(myList)
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
>>> print(myString)
Hello World
0
string : str = "".join(["a", "b", "c"])

Output : "a,b,c"

string : str = "-".join(["a", "b", "c"])

Output : "a-b-c"

Simply, this function is used to convert from a list to a string. This function takes two arguments wich are : the list of elements and the character that will seperate the elements of the list ; as default, the character is a quote "," .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.