Dive Into Python Python for experienced programmers |

## 1.14. Joining lists and splitting strings

You have a list of key-value pairs in the form `key=value`, and you want to join them into a single string. To join any list of strings into a single string, use the `join` method of a string object.

Example 1.35. Joining a list in `buildConnectionString`

        return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

One interesting note before we continue. I keep repeating that functions are objects, strings are objects, everything is an object. You might have thought I meant that string variables are objects. But no, look closely at this example and you'll see that the string `";"` itself is an object, and you are calling its `join` method.

Anyway, the `join` method joins the elements of the list into a single string, with each element separated by a semi-colon. The delimiter doesn't have to be a semi-colon; it doesn't even have to be a single character. It can be any string.

`join` only works on lists of strings; it does not do any type coercion. `join`ing a list that has one or more non-string elements will raise an exception.

Example 1.36. Output of `odbchelper.py`

    >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
    >>> ["%s=%s" % (k, v) for k, v in params.items()]
    ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
    >>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])
    server=mpilgrim;uid=sa;database=master;pwd=secret

This string is then returned from the `help` function and printed by the calling block, which gives you the output that you marveled at when you started reading this chapter.

Historical note. When I first learned Python, I expected `join` to be a method of a list, which would take the delimiter as an argument. Lots of people feel the same way, and there's a story behind the `join` method. Prior to Python 1.6, strings didn't have all these useful methods. There was a separate `string` module which contained all the string functions; each function took a string as its first argument. The functions were deemed important enough to put onto the strings themselves, which made sense for functions like `lower`, `upper`, and `split`. But many hard-core Python programmers objected to the new `join` method, arguing that it should be a method of the list instead, or that it shouldn't move at all but simply stay a part of the old `string` module (which still has lots of useful stuff in it). I use the new `join` method exclusively, but you will see code written either way, and if it really bothers you, you can use the old `string.join` function instead.

You're probably wondering if there's an analogous method to split a string into a list. And of course there is, and it's called `split`.

Example 1.37. Splitting a string

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

| `split` reverses `join` by splitting a string into a multi-element list. Note that the delimiter (“`;`”) is stripped out completely; it does not appear in any of the elements of the returned list.
| `split` takes an optional second argument, which is the number of times to split. (“"Oooooh, optional arguments...” You'll learn how to do this in your own functions in the next chapter.)

`anystring.`split`(delimiter, 1)` is a useful technique when you want to search a string for a substring and then work with everything before the substring (which ends up in the first element of the returned list) and everything after it (which ends up in the second element).

Further reading

- Python Knowledge Base answers common questions about strings and has lots of example code using strings.
- Python Library Reference summarizes all the string methods.
- Python Library Reference documents the `string` module.
- The Whole Python FAQ explains why `join` is a string method instead of a list method.