Home > Dive Into Python > Getting To Know Python > Joining lists and splitting strings | << >> |
diveintopython.org | |
Python for experienced programmers | |
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.32. Joining a list in buildConnectionString
return ";".join(["%s=%s" % (k, params[k]) for k in params.keys()])
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. joining a list that has one or more non-string elements will raise an exception. |
Example 1.33. Output of odbchelper.py
>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} >>> ["%s=%s" % (k, params[k]) for k in params.keys()] ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret'] >>> ";".join(["%s=%s" % (k, params[k]) for k in params.keys()]) 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. The reason for the seeming inconsistency is purely historical. Prior to Python 1.6, strings didn't have all these useful methods. There was a separate string module which held all the string functions; each function took a string as its first argument. The functions were deemed important enough, so they migrated onto the strings themselves, which was certainly a Good Thing™. But, for better or worse, join came with them, and here we are.
If you're smart (and you are), 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.34. 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(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), everything after it (which ends up in the second element), or both. |
« Mapping lists | Summary » |