I'm a Python noob and I need some help for a simple problem.
What I need to do is create a list with 3 items and add spaces before and after every item.
For example: l1 = ['a', 'bb', 'c']
should be transformed into: [' a ',' bb ',' c ']
I was trying to write something like this:
lst = ['a', 'bb', 'c']
for a in lst:
print ' a '
...and so on for the other elements, but I get a syntax error. Can anyone suggest me a working way to do this? Thanks.
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']
In this post, we will see how to add space in Python. Specifically, we will cover the following topics:
- Add a space between variables in print[]
- Add spaces to the left and right sides of a string
- Add spaces at the beginning of a string
- Add spaces at the end of a string
- Add spaces between characters in a string
- Add a space between string items
So, without further ado, let’s get started.
Add a space between variables in print[]
Often, when we print variables, we want to separate them by a space to make them more readable. If variables in print[] are separated by a comma, a space gets automatically added between them. Let’s take an example.
a = 4 b = 3 print[a, b] fname = "ashton" lname = "agar" print["His name is", fname, lname]
Output
4 3 His name is ashton agar
Next, we will see how to add spaces at different places in a string.
Add spaces to the left and right sides of a string
To add space to the left and right sides, we can use the center[] method. It takes the length of the returned string. Moreover, it is invoked on a string and outputs a new one. Consider the following example.
name = "ashton" format = name.center[len[name]+8] print[format]
Output
ashton
In the above example, the new string’s length is 14, i.e., 8 for spaces and 6 for name length. 4 spaces get added to the left and 4 to the right.
Add spaces at the beginning of a string
To add spaces at the beginning only, we can use the rjust[] method. It right aligns the given string. It takes the length of the formatted string like the center[] method does. Let’s see.
name = "ashton" format = name.rjust[len[name]+4] #add four spaces at the beginning print[format]
Output
ashton
Add spaces at the end of a string
The ljust[] method, on the other hand, can be used to add spaces to the end. Let’s see.
name = "ashton" format = name.ljust[len[name]+4] #add four spaces to the end print[format]
Output
ashton
Add spaces between characters in a string
Let’s see how to separate each character in a string by a space. For that, we can use the join[] method.
The join[] method takes an iterable [list, string] and returns a string in which all items of an iterable are joined by a separator.
Here, we will pass our input string to join[] and use space as a separator. Let’s see.
string = "cricket" formatted = " ".join[string] #separate each character by a space print[formatted]
Output
c r i c k e t
As you can see, we add space between each character of the string.
Add a space between string items
Using the join[] method, we can also add space between two or more string items. Let’s see.
str1 = "cricket" str2 = "football" str3 = "hockey" string = " ".join[[str1, str2, str3]] print[string]
Output
cricket football hockey
In the above example, we pass a list containing string items to the join[] method. It returns a new string having the list items joined by a space.
If you only want to display [print] strings in such a way, you can use the print[] method and separate them using a comma.
Hey guys! It’s me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Don’t let anyone tell you that this can’t be done. Sky’s the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!