How to add space between two strings in python

>>> item1="eggs"
>>> item2="sandwich"

>>> print(item1+item2)

>>> Output: eggssandwich

My main goal is to put a space between eggs and sandwich.

But i'm unsure on how to. Any help would be appreciated

How to add space between two strings in python

Avinash Raj

168k25 gold badges214 silver badges261 bronze badges

asked Mar 1, 2016 at 12:46

1

Use .join():

print(" ".join([item1, item2]))

The default for print, however, is to put a space between arguments, so you could also do:

print(item1, item2)

Another way would be to use string formatting:

print("{} {}".format(item1, item2))

Or the old way:

print("%s %s" % (item1, item2))

answered Mar 1, 2016 at 12:48

How to add space between two strings in python

zondozondo

19.2k7 gold badges42 silver badges83 bronze badges

Simply!

'{} {}'.format(item1, item2)  # the most prefereable

or

'%s %s' % (item1, item2)

or if it is just print

print(item1, item2)

for dynamic count of elements you can use join(like in another answer in the tread).

Also you can read how to make really flexible formatting using format language from the first variant in official documentation: https://docs.python.org/2/library/string.html#custom-string-formatting

Update: since f-strings were introduced in Python 3.6, it is also possible to use them:

f'{item1} {item2}'

aberger

2,2593 gold badges14 silver badges28 bronze badges

answered Mar 1, 2016 at 12:48

Andrey RusanovAndrey Rusanov

4,1772 gold badges30 silver badges51 bronze badges

0

Just add the space!

print(item1 + ' ' + item2)

answered Mar 1, 2016 at 12:48

csskocssko

2,9361 gold badge17 silver badges21 bronze badges

1

# works every time
print(item1, item2)
# Only works if items 1 & 2 are strings.
print(item1 + " " + item2)

How to add space between two strings in python

Rishab P

1,5436 silver badges18 bronze badges

answered Apr 12, 2020 at 17:54

How to add space between two strings in python

1

Here are three easy solutions to add a space.

Add a space in between, but as noted above this only works if both items are strings.

print("eggs" + " " + "sandwich")

Another simple solution would be to add a space to end of eggs or beginning of sandwich.

print("eggs " + "sandwich")
print("eggs" + " sandwich")

These will all return the same result.

answered Dec 26, 2020 at 12:20

How to add spaces between two concatenated strings? ****Here you got answer for the above question.** **I think so this is simple way to add spaces between two concatenated strings.****

college ='geethanjaliinstituteofscienceandtechnology'
 name =" " 'jaanu'
 test1 =college+name
 print(test1)

output:geethanjaliinstituteofscienceandtechnology jaanu

answered Jan 24, 2020 at 5:17

How to add space between two strings in python

>>> item1="eggs"
>>> item2="sandwich"

>>> print(f"{item1} {item2}")

How to add space between two strings in python

khelwood

52.8k13 gold badges79 silver badges99 bronze badges

answered Apr 27, 2021 at 17:15

1

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

How do you add two strings in space?

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 write spaces in Python?

In Python, characters that are used for spacing are called as whitespace characters..
' ' – Space..
'\t' – Horizontal tab..
'\v' – Vertical tab..
'\n' – Newline..
'\r' – Carriage return..
'\f' – Feed..

How does \n work in Python?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.

How do you put a space at the end of a string in Python?

Use the str. ljust() method to add spaces to the end of a string, e.g. result = my_str. ljust(6, ' ') . The ljust method takes the total width of the string and a fill character and pads the end of the string to the specified width with the provided fill character.