Skip to content

Instantly share code, notes, and snippets.

@zeph1rus
Created May 25, 2021 19:35
Show Gist options
  • Save zeph1rus/a6404ca68b52d32dc2a3b0674dfb466d to your computer and use it in GitHub Desktop.
Save zeph1rus/a6404ca68b52d32dc2a3b0674dfb466d to your computer and use it in GitHub Desktop.
Python f string none value shows as 'None' in string
def rnn(i: str) -> str:
"""
Python hack. In f-strings, python will interpret '' or "" as "None" instead of, well None, and
thus you get None in text which helps nobody in url strings. This uses an iterator join on a
list comprehension to ensure we return no text at all if the value is none. Thanks python.
:param i: String that may be None or "" or an actual value
:type i: str
:return: the text if it is there else, specifically '' (not None or null like object)
:rtype: str
"""
return ''.join(a for a in {i} if i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment