Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting 1 space between two strings using python

Tags:

python

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?

like image 819
user1521069 Avatar asked Jul 13 '12 12:07

user1521069


2 Answers

You can use join to concatenate your strings together with your selected delimiter.

a = "abcd"
b = "xyz"
c = " ".join([a, b])
like image 76
Christian Witts Avatar answered Oct 14 '22 20:10

Christian Witts


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")

like image 36
Levon Avatar answered Oct 14 '22 18:10

Levon