The problem with +
is that the second argument must be a string, otherwise an error is thrown. If that's not an issue, using +
will work. However, there are other, usually more idiomatic ways:
1.) the print function can take multiple arguments, which get concatenated with a space:
print('Executing against', env)
2.) "old-style" interpolation:
print('Executing against %s' % env)
The right-hand side may also take multiple arguments in a tuple. So to protect against the possibility of the argument to be a tuple itself, you usually always put your variable in a tuple:
print('Executing against %s' % (env,))
3.) The more modern way is to use .format
and its ecosystem (there's magic __format__
methods and whatnot you usually don't need to know about but gives you great flexibility):
print('Executing against {}'.format(env))
4.) Python 3.6 and upwards: Syntactic sugar for .format()
. You can now directly put variables, and even whole expressions inside the string by making it a format-string (prefix it with f
):
print(f'Executing against {env}')