Python: how to concatenate strings when one is None


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:

Traceback (most recent call last):
File "", line 451, in 
lambda_handler(None, None)
File "", line 442, in lambda_handler
print("Some URL: " + some_url)
TypeError: cannot concatenate 'str' and 'NoneType' objects

Python should not be so dogmatic.

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


You can do:

print("Some URL: ")

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 *