LA_
LA_

Reputation: 20409

Is there any function to convert None to empty string?

Something my string var is equal to None. I don't want to have if statements to verify the value. Is there any simple way (like str(my_var)) which will return string value if this is not None and '' if None?

Upvotes: 1

Views: 2051

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318508

You can use foo or '', but in case foo is any other falsy value (0, an empty list, etc.) it will result in an empty string, too.

Another way would be '' if foo is None else foo

Upvotes: 11

Related Questions