Combine several variables of different data types into one string
-
10-07-2019 - |
Question
I'd like to combine 6 variables that are of different types in my view...I thought the concatenation '+' will work but I get an error when I use it. I'd like the end result to be like this:
var1 var2 var3 var4 (var5 var6)
How do I go about this?
Solution
I'm assuming the problem you had with using +
is that not all of the variables are strings. Use the following but replace %s
with an applicable formatter from this list.
output_string = "%s %s %s %s (%s %s)" % (var1, var2, var3, var4, var5, var6)
Another option is to pass them to your template and output them there:
{{ var1 }} {{ var2 }} {{ var3 }} {{ var4 }} ({{ var5 }} {{ var6 }})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow