Python: how to concatenate strings when one is None

TLDR

Use: print "Some URL: %s" % (some_url)

rather than: print("Some URL " + some_url)

 

 

Try to print out a variable that’s actually None rather than a string, e.g. with

print("Some URL " + some_url)

will result in:

Python should not be so dogmatic.

Why Python sucks – why I hate Python: a Python rant

 

You can do:

but that’s dodging the problem. It assumes that you’re aware of the potential bug before you code. Also, you end up with the text on two separate lines.

 

The solution is to use this syntax:

print "Some URL: %s" % (some_url)

 

 

 

 

 

Leave a Reply

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