How to concatenate two strings in python with space

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?

How to concatenate two strings in python with space

msw

41.8k9 gold badges83 silver badges108 bronze badges

asked Jul 13, 2012 at 12:58

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

answered Jul 13, 2012 at 12:59

LevonLevon

131k33 gold badges197 silver badges186 bronze badges

3

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

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

answered Jul 13, 2012 at 13:00

How to concatenate two strings in python with space

Christian WittsChristian Witts

11k1 gold badge31 silver badges44 bronze badges

Since Python 3.6, you can use f-strings to join two strings with ease. The code is more succinct and the variables are referenced directly within the context of the string.

a = 'hello'
b = 'world'
c = f'{a} {b}'

print(c)

The above code would output the following:

hello world

The .join() function is still the way to go when you have many strings or where the number of strings may vary.

answered Mar 1, 2019 at 16:33

How to concatenate two strings in python with space

noddynoddy

2,9073 gold badges24 silver badges20 bronze badges

Python supports the string formatting operations and a template system (the latter is technically a simple but powerful class) as part of the string module. While the plus operator does its work, the lack of string formatting can influence a lot the readability of the code. A basic example for string formatting:

c = '%s %s' % (a, b)

How to concatenate two strings in python with space

georg

206k48 gold badges292 silver badges373 bronze badges

answered Jul 13, 2012 at 13:28

How to concatenate two strings in python with space

VidulVidul

9,7062 gold badges17 silver badges20 bronze badges

2

Not the answer you're looking for? Browse other questions tagged python or ask your own question.

How do you concatenate two strings with spaces?

There are two ways to do this:.
Add double quotation marks with a space between them " ". For example: =CONCATENATE("Hello", " ", "World!")..
Add a space after the Text argument. For example: =CONCATENATE("Hello ", "World!"). The string "Hello " has an extra space added..

How do you concatenate a space in a string in Python?

If you want a space in the resulting string, there must be a space in one of the two original strings. The + operator can concatenate two string values into a new string value ('Hello ' + 'World!' to 'Hello World!' ), just like it could add two integer values into a new integer value (2 + 2 to 4).

How do you concatenate two strings without space in Python?

We can perform string concatenation using following ways:.
Using + operator..
Using join() method..
Using % operator..
Using format() function..
Using , (comma).

Can strings have spaces in Python?

Python String isspace() method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as: ' ' – Space.