Concat string if condition, else do nothing

Try something below without using else. It works by indexing empty string when condition False (0) and indexing string c when condition True (1)

something = a + b + ['', c][condition]

I am not sure why you want to avoid using else, otherwise, the code below seems more readable:

something = a + b + (c if condition else '')

It is possible, but it's not very Pythonic:

something = a + b + c * condition

This will work because condition * False will return '', while condition * True will return original condition. However, You must be careful here, condition could also be 0 or 1, but any higher number or any literal will break the code.


This should work for simple scenarios -

something = ''.join([a, b, c if condition else ''])

Is there a nice way to do it without the else option?

Well, yes:

something = ''.join([a, b])
if condition:
    something = ''.join([something, c])

But I don't know whether you mean literally without else, or without the whole if statement.