Empty String to None Conversion - Python Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report We are given a empty string we need to convert string to None. For example s = "" we are given a string we need to convert that to None so that out output becomes None.Using TernaryTernary operator in Python allows us to perform conditional expressions in a single line. We can use it to convert an empty string to None by checking if the string is empty and assigning None if true or keeping original string otherwise. Python s = "" # Use a ternary operator to convert the empty string to None s = None if s == "" else s print(s) OutputNone Explanation:Ternary operator checks if s is an empty string (s == ""). If true, it assigns None to s; otherwise, it keeps the original value of s.Since s is an empty string the result is None.Using or Operatoror operator in Python returns first truthy value it encounters or last value if none are truthy. We can use it to convert an empty string to None as an empty string is false and will result in None. Python s = "" # Use the or operator to convert the empty string to None s = s or None print(s) OutputNone Explanation:or operator evaluates s and, since an empty string is false it assigns None to s.If s had a truthy value it would have been returned instead of None.Using if Statementif statement can be used to explicitly check if the string is empty and if true assign None to the variable. Otherwise original string is retained. This makes logic clear and straightforward. Python s = "" # Check if the string is empty, and if so, assign None to s if s == "": s = None print(s) OutputNone Explanation:if statement checks if the string s is empty (s == ""). If true None is assigned to s.Since s is an empty string condition is met and output is None.Using map()map() function applies a given function to each item in an iterable producing a new iterable with the results. It can be used to convert empty strings to None by defining a function that checks for empty strings and replaces them. Python a = ["", "hello", "", "world"] # Use map() with a lambda function to convert empty strings to None a = list(map(lambda x: None if x == "" else x, a)) print(a) Output[None, 'hello', None, 'world'] Explanation:map() applies lambda function to each element in the list a lambda function returns None for empty strings and original element for non-empty strings.list() converts result from map() back into a list and output shows that empty strings have been replaced by None Comment More info M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like