So in order to achieve a desired output, we should first know how the function works.
The syntax for join()
method as described in the python documentation is as follows:
string_name.join(iterable)
Things to be noted:
string
concatenated with the elements of iterable
. The separator between the elements being the string_name
. iterable
will raise a TypeError
Now, to add white spaces, we just need to replace the string_name
with a " "
or a ' '
both of them will work and place the iterable
that we want to concatenate.
So, our function will look something like this:
' '.join(my_list)
But, what if we want to add a particular number of white spaces
in between our elements in the iterable
?
We need to add this:
str(number*" ").join(iterable)
here, the number
will be a user input.
So, for example if number=4
.
Then, the output of str(4*" ").join(my_list)
will be how are you
, so in between every word there are 4 white spaces.