>> b = "xyz" >>> c = a + b >>> c abcdxyz How can I get abcd ...">

Setting 1 space between two lines using python

I have two lines:

>>> a = "abcd" >>> b = "xyz" >>> c = a + b >>> c abcdxyz 

How can I get abcd xyz as a result instead of adding a and b ?

+4
source share
4 answers

Just add a space between the two lines:

 a = "abcd" b = "xyz" c = a + " " + b # note the extra space concatenated with the other two print c 

it will give you

 abcd xyz 

You can use a function like .join() , but for something so short that it would seem almost counter-intuitive (and IMO "overkill"). Ie, first create a list with two lines, then call a function with this list and use returning this function to the print statement ... when you can just combine the required space with 2 lines. Seems semantically clearer too.

Based on: "Simple is better than complex." (Zen Python "import this")

+11
source

You can use concatenation to concatenate your lines with the selected delimiter.

 a = "abcd" b = "xyz" c = " ".join([a, b]) 
+8
source

Python supports string formatting operations and a template system (the latter is a technically simple but powerful class) as part of a string module. Although the plus operator does its job, the lack of line formatting can greatly affect code readability. Basic example of string formatting:

 c = '%s %s' % (a, b) 
+3
source

If you only need to print a space between them, you can use this:

 a = "hi" b = "how ru" print a,b 
-1
source

All Articles