How to join multiple Strings with possibly None values in Python

To join multiple Strings with possibly None values ​​in Python, I perform a string check for None or use the filter() function to filter out None. If you are interested in this topic, the following article will definitely clear your problems.

Join multiple Strings with possibly None values in Python

Check the NoneType object.

You can use the Authentication operator to check if a variable can validly perform a concatenation.

Example:

  • Declare a variable whose value is None.
  • Use the Authentication operator. If the variable contains the value None, execute the if statement; otherwise, you can join two objects together.
  • You can replace the ‘is’ operator with the ‘is not’ operator (substitute statements accordingly).
firstStr = 'visit'
secondStr = 'learnshareit website'
thirdStr = None

# Create an empty string that stores the value
newStr = ''

# Use the 'is' operator
if thirdStr is None:
    print('Object is None')
else:
    print('Object is not None')
    newStr = firstStr + secondStr + thirdStr

print(newStr)

Output:

Object is None

You can use the relational operator ‘!=’ for error handling.

The ‘!=’ operator compares the values ​​of the arguments: if they are different, it returns True. If equal, returns False.

Example:

firstStr = 'visit'
secondStr = 'learnshareit website'
thirdStr = None

# Create an empty string that stores the value
newStr = ''

# Use the relational operator '!='
if thirdStr != None:
    print('Object is not None')
    print(firstStr + secondStr + thirdStr)
else:
    print('Object is None')

Output:

Object is None

Use the isinstance() function to check NoneType. 

Example:

firstStr = 'visit'
secondStr = 'learnshareit website'
thirdStr = None

# Use the isinstance() function to check the None object
if isinstance(thirdStr, type(None)) is True:
    print('Object is None')
else:
    result = firstStr + secondStr + thirdStr
    print(result)

Output:

Object is None

Use the filter() function in conjunction with the join() function.

Syntax:

filter(func, iterable)

Parameters:

  • func: is a conditional function to check whether the elements in the array are true or false, func can only return True or False.
  • interable: is the iterable to be filtered, can be set, list, tuple, or container.

The filter() function will only return the values ​​that the condition in the func accepts (that is, True).

Syntax:

str.join(object)

Parameters:

  • str: Delimiter for string concatenation.
  • object: List or tuple containing elements that are strings to be concatenated.

The join() function returns an object that is a character string of the elements of the specified list or tuple joined together by a delimiter.

Example:

  • I initialize four strings, one with the value None.
  • I use the filter() function to filter out the None value.
  • Then the valid strings will be concatenated by the join() function.
firstStr = 'visit'
secondStr = 'learnshareit'
thirdStr = 'website'
fourthStr = None

# Use the filter() function to filter out the value None.
# The join() function will concatenate valid strings together.
strMerge = ' '.join(filter(None, [firstStr, secondStr, thirdStr, fourthStr]))

print(strMerge)

Output:

visit learnshareit website

Summary:

The thread joining multiple Strings with possibly None values in Python may end here. You can get the filter() function to do this. Please comment below if you have any other questions or ideas for this article.

Leave a Reply

Your email address will not be published. Required fields are marked *