How To Convert None To An Empty String In Python

How to convert None to an Empty string in Python

This article will help you learn about how to convert none to an empty string in Python. There are some solutions we have effectively tested. Follow this article to understand better.

Convert None to an Empty string in Python

In Python, ‘None’ is a specific object indicating that the object is missing the presence of a value. The Python Null object is the singleton None. 

Note: constant ‘None’ is defined as False, so when combined using the same logical operator or always returns False.

To convert none to an empty string, we have the following ways:

Use lambda function

Lambda, or anonymous function, can be considered a mini function that can be created flexibly. The function can execute an expression for some arguments as a normal function.

Syntax:

lambda arguments: expression

Parameters:

  • arguments: any objects need to be applied to the expression.
  • expression: function executed.

Example:

# The original string contains None
orgStr = ["Learn", None, "Share", None, None, "IT"] 
  
# Using lambda function to convert None to the empty string
convNone = lambda i : i or ''
resultStr = [convNone(i) for i in orgStr]

print("Original string is : ", orgStr)
print("Result string : ", resultStr)

Output:

Original string is :  ['Learn', None, 'Share', None, None, 'IT']
Result string :  ['Learn', '', 'Share', '', '', 'IT']

The or operator compares the function object returned by the lambda function with spaces and replaces None values with the empty string.

Use the str() method 

You can use the str() function to convert integers to strings. This way will solve the problem.

Syntax:

str(object)

Parameters:

  • object: is an object that can be displayed as a string.

The str() function returns a string.

Example:

# The original string contains None
orgStr = ["Learn", None, "Share", None, None, "IT"] 
  
# Using str() to convert None to the empty string
resultStr = [str(i or '') for i in orgStr]

print("Original string is : ", orgStr)
print("Result string : ", resultStr)

Output:

Original string is :  ['Learn', None, 'Share', None, None, 'IT']
Result string :  ['Learn', '', 'Share', '', '', 'IT']

The or operator defaults to ‘None’, which is False, so it returns an empty string, and the str() function prints it out: the original string element, and ‘None’ converts to an empty string.

Use the logical operator or

You can use the or operator to convert.

Example: If the value of the variable is “None”, the “or” operator will return an empty string

orgStr = None

# Using or operator to convert None to the empty string
resultStr = orgStr or ''

print("Original string is : ", orgStr)
print("Result string is : ", resultStr)

Output:

Original string is :  None
Result string is :  

The or operator defaults to ‘None’, which is False, so it returns an empty string.

Summary

Here are the solutions that can help you convert none to an empty string in Python. If you have any questions about this article, leave a comment below. I will answer your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

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