0
How to print space in between two list elements in a single line
words =[ 1, 2, 3] print( words[1], words[2]) how to print space in betweeen words 1 and words 2
1 Answer
+ 1
What you have does print the space between the elements. The result is "2 3".
By default the python print() method prints a space between comma separated arguments.
If you want to change the separator you add "sep=" argument as the last argument of the function call.
Like so to remove the space:
print( words[1], words[2], sep="") //prints "23"
Or this to separate each element with a period:
print( words[1], words[2], sep=".") //prints "2.3"
Use whichever character(s) you need to separate them with.
You can also use the "end=" argument to specify the character(s) you would like to add to the end of the string. This can be used by itself or along with the "sep=" argument.
print( words[1], words[2], sep="--", end=" ;-)") //prints "2--3 ;-)"
Alternately you can use string formatting:
print( "words[1] is {}, words[2] is {}".format(words[1], words[2]))
// outputs-> words[1] is 2, words[2] is 3