>>> 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
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
zondozondo
19.2k7 gold badges42 silver badges83 bronze badges
Simply!
'{} {}'.format(item1, item2) # the most prefereableor
'%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: //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)
Rishab P
1,5436 silver badges18 bronze badges
answered Apr 12, 2020 at 17:54
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
>>> item1="eggs" >>> item2="sandwich" >>> print(f"{item1} {item2}")
khelwood
52.8k13 gold badges79 silver badges99 bronze badges
answered Apr 27, 2021 at 17:15
1