View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given list of Strings, task is to add a space before sequence which begin with capital letters.
Input : test_list = [“gfgBest”, “forGeeks”, “andComputerScienceStudents”]
Output : [‘gfg Best’, ‘for Geeks’, ‘and Computer Science Students’]
Explanation : Words segregated by Capitals.Input : test_list = [“ComputerScienceStudentsLoveGfg”]
Output : [‘Computer Science Students Love Gfg’]
Explanation : Words segregated by Capitals.
Method #1 : Using loop + join[]
This is one of the ways in which this task can be performed. In this, we perform the task of iterating all the stings and then all the characters before adding space using loop in brute force manner. The isupper[] is used to check for capital character.
Python3
test_list
=
[
"gfgBest"
,
"forGeeks"
,
"andComputerScience"
]
print
[
"The original list : "
+
str
[test_list]]
res
=
[]
for
ele
in
test_list:
temp
=
[[]]
for
char
in
ele:
if
char.isupper[]:
temp.append[[]]
temp[
-
1
].append[char]
res.append[
' '
.join[''.join[ele]
for
ele
in
temp]]
print
[
"The space added list of strings : "
+
str
[res]]
Output
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
Method #2 : Using regex[] + list comprehension
The combination of above functions can also be used to solve this problem. In this we employ regex code to check for upper case letters and perform space addition and joining using list comprehension.
Python3
import
re
test_list
=
[
"gfgBest"
,
"forGeeks"
,
"andComputerScience"
]
print
[
"The original list : "
+
str
[test_list]]
res
=
[re.sub[r
"[\w][[A-Z]]"
, r
"\1 \2"
, ele]
for
ele
in
test_list]
print
[
"The space added list of strings : "
+
str
[res]]
Output
The original list : ['gfgBest', 'forGeeks', 'andComputerScience'] The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
>>> 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
169k25 gold badges214 silver badges262 bronze badges
asked Mar 1, 2016 at 12:46
0
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.3k7 gold badges43 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: //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,2052 gold badges31 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
There are a lot of ways ;] :
print[f'Hello {firstname} {lastname}']
Or
print["Hello", firstname, lastname]
Or
print["Hello", firstname + ' ' + lastname]
Or
print[' '.join[["Hello", firstname , lastname]]]
Or
[print[i, end=' '] for i in ["Hello", firstname, lastname]]
answered Sep 19 at 18:41
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
53k13 gold badges79 silver badges99 bronze badges
answered Apr 27, 2021 at 17:15
1