I have two strings:
>>> a = "abcd"
>>> b = "xyz"
>>> c = a + b
>>> c
abcdxyz
How can I get abcd xyz
as a result instead when adding a
and b
?
You can use join to concatenate your strings together with your selected delimiter.
a = "abcd"
b = "xyz"
c = " ".join([a, b])
Simply just add a space between the two strings:
a = "abcd"
b = "xyz"
c = a + " " + b # note the extra space concatenated with the other two
print c
this will give you
abcd xyz
You can use a function such as .join()
, but for something so short, that would seem almost counter-intuitive (and IMO "overkill"). I.e., first create a list with the two strings, then call a function with that list and use the return of that function in a print statement ... when you can just concatenate the needed space with the 2 strings. Seems semantically more clear too.
Based on: "Simple is better than complex." (The Zen of Python "import this")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With