Skip to content

How to Join Strings in Python While Handling None and Empty Values

Learn how to safely join strings in Python while excluding None values, empty strings, and whitespace-only entries using filter(), generator expressions, and str.join().

Joining a collection of strings is a common task, but errors or unexpected results can occur if the collection contains None values or empty strings.

This guide explores robust methods for joining strings in Python while gracefully handling None and empty values, using filter(), generator expressions, and str.join().

Joining Strings, Excluding None Values

When you have a list or other iterable that might contain None, you need to filter these out before joining.

The filter() function with None as the first argument provides a concise way to remove all falsy values (including None and empty strings) before joining:

str_1 = 'Tom'
str_2 = None
str_3 = 'Nolan'
str_4 = ''      # Empty string (also falsy)

my_data = [str_1, str_2, str_3, str_4]

result = ', '.join(filter(None, my_data))
print(result)   # Output: Tom, Nolan
  • filter(None, my_data) creates an iterator yielding only the truthy elements from my_data.
  • str.join() then concatenates these truthy strings.

Using a Generator Expression

A generator expression offers an explicit alternative to filter specifically for None:

str_1 = 'Tom'
str_2 = None
str_3 = 'Nolan'
str_4 = ''      # Empty string (will be included here)

my_data = [str_1, str_2, str_3, str_4]

result = ' '.join(value for value in my_data if value is not None)
print(result)   # Output: Tom Nolan
  • The generator expression (value for value in my_data if value is not None) explicitly yields values that are not None. Empty strings will be included in this case.

Handling Non-String Values with map()

If your list might contain non-string values in addition to None, convert them to strings before filtering and joining:

data_mixed = ['Tom', None, 'Nolan', 500, True, '']

result = ', '.join(
    filter(None, map(str, data_mixed))
)
print(result)  # Output: Tom, Nolan, 500, True
  • map(str, data_mixed) converts all items (including None and True) to their string representations ('None', 'True').
  • filter(None, ...) then filters out the empty string '' (but keeps 'None' and 'True' because they are truthy strings).

Joining Strings, Excluding Empty Strings and None

Often, you want to exclude both None and empty strings (or strings containing only whitespace).

Using filter(None, ...)

As shown previously, filter(None, ...) naturally handles both None and empty strings because both are falsy:

my_strings = ['apple', '', 'banana', None, 'kiwi', '  '] # Includes whitespace string

result = ' '.join(filter(None, my_strings))
print(result)   # Output: apple banana kiwi

Using a Generator Expression with strip()

For more explicit control and to also exclude strings containing only whitespace, use a generator expression with strip():

my_strings = ['apple', '', 'banana', None, ' ', 'kiwi', '\n']

result = ' '.join(item for item in my_strings if item and item.strip())
print(result)  # Output: apple banana kiwi
  • if item: Filters out None.
  • if item.strip(): Filters out empty strings and strings containing only whitespace (because strip() returns an empty string for these, which is falsy).
Navigation