Last updated: Apr 8, 2024
Reading timeยท5 min
To join multiple strings with possibly None values:
join()
method with an optional delimiter.filter()
function to filter out any None
values.join()
method returns a string which is the concatenation of the
strings in the iterable.str_1 = 'Bobby' str_2 = None str_3 = 'Hadz' result = ', '.join(filter(None, [str_1, str_2, str_3])) print(result) # ๐๏ธ "Bobby, Hadz"
We passed a comma as a delimiter but you can adjust it depending on your use case.
Here is an example that uses a space as a delimiter.
str_1 = 'Bobby' str_2 = None str_3 = 'Hadz' result = ' '.join(filter(None, [str_1, str_2, str_3])) print(result) # ๐๏ธ "Bobby Hadz"
If you don't need a delimiter between the strings, call the join()
method on
an empty string.
str_1 = 'Bobby' str_2 = None str_3 = 'Hadz' result = ''.join(filter(None, [str_1, str_2, str_3])) print(result) # ๐๏ธ "BobbyHadz"
The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
If some of the values you are joining are not of type string, use the map()
function to convert them.
str_1 = 'Bobby' str_2 = None str_3 = 'Hadz' str_4 = 500 result = ', '.join( filter( None, map(str, [str_1, str_2, str_3, str_4]) ) ) print(result) # ๐๏ธ "Bobby, None, Hadz, 500"
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
The map()
function gets called with each element in the list and passes each
value to the str() class.
The string the join()
method is called on is used as the separator between
elements.
my_list = ['a', 'b', 'c'] my_str = '-'.join(my_list) print(my_str) # ๐๏ธ "a-b-c"
We used the filter()
function to filter out any None
values from the list.
str_1 = 'Bobby' str_2 = None str_3 = 'Hadz' result = ', '.join(filter(None, [str_1, str_2, str_3])) print(result) # ๐๏ธ "Bobby, Hadz"
The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.
None
for the function argument, all falsy elements of the iterable are removed.All values that are not truthy are considered falsy. The falsy values in Python are:
None
and False
.0
(zero) of any numeric type""
(empty string), ()
(empty tuple), []
(empty list), {}
(empty dictionary), set()
(empty set), range(0)
(empty
range).filter()
You can also use a
generator expression
instead of the filter()
function.
str_1 = 'Bobby' str_2 = None str_3 = 'Hadz' result = ' '.join( value for value in [str_1, str_2, str_3] if value is not None ) print(result) # ๐๏ธ Bobby Hadz
We used a generator expression to iterate over the list of values.
On each iteration, we check if the current value is not None and return the result.
The str.join()
method only gets called with not-None values, so the code
sample achieves the same result as using the filter()
function.
If you need to join multiple strings if they aren't empty:
filter()
function to filter out all falsy values.join()
method on a string separator.join()
method.my_strings = ['apple', '', 'banana', '', 'kiwi', ''] result = ' '.join(filter(None, my_strings)) print(result) # ๐๏ธ 'apple banana kiwi'
The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.
If you pass None
for the function argument, all falsy elements of the iterable
are removed.
my_strings = ['apple', '', 'banana', '', 'kiwi', '', None, 0] # ๐๏ธ ['apple', 'banana', 'kiwi'] print(list(filter(None, my_strings)))
Notice that all of the falsy values from the list are filtered out in the result.
All values that are not truthy are considered falsy. The falsy values in Python are:
None
and False
.0
(zero) of any numeric type""
(empty string), ()
(empty tuple), []
(empty list), {}
(empty dictionary), set()
(empty set), range(0)
(empty
range).The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
TypeError
if there are any non-string values in the iterable.If your list contains numbers or other types, convert all of the values to
string before calling join()
.
my_strings = ['apple', 1, 'banana', 2, 'kiwi', '', None, 0] result = ' '.join(map(str, filter(None, my_strings))) print(result) # ๐๏ธ 'apple 1 banana 2 kiwi'
The string the method is called on is used as the separator between elements.
my_strings = ['apple', '', 'banana', '', 'kiwi', ''] result = '-'.join(filter(None, my_strings)) print(result) # ๐๏ธ 'apple-banana-kiwi'
If you don't need a separator and just want to join the iterable's elements into
a string, call the join()
method on an empty string.
my_strings = ['a', '', 'b', '', 'c', ''] result = ''.join(filter(None, my_strings)) print(result) # ๐๏ธ 'abc'
An alternative approach to join strings only if they aren't empty is to use a generator expression.
my_strings = ['apple', '', 'banana', '', ' ', 'kiwi', ''] result = ' '.join(item for item in my_strings if item.strip()) print(result) # ๐๏ธ 'apple banana kiwi'
The str.strip() method returns a copy of the string with the leading and trailing whitespace removed.
We passed a generator expression to the str.join()
method.
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.
You can learn more about the related topics by checking out the following tutorials: