How do i print an array in one line in python?
I'm new to python and I'm trying to scan multiple numbers separated by spaces (let's assume '1 2 3' as an example) in a single line and add it to a list of int. I did it by using: Show
Apparently it works, since when I type in '1 2 3' and do a
But I want to print it in a single line without the brackets, and with a space in between the numbers, like this:
I've tried doing:
But I get an error:
How can I print the list of ints (assuming my first two lines of code are right) in a single line, and without the brackets and commas?
asked Jun 4, 2016 at 0:30
1 Yes
that is possible in Python 3, just use
This will print the list separated by spaces. (where
user2314737 25.2k18 gold badges94 silver badges106 bronze badges answered Aug 19, 2018 at 5:35
1 You want to say
The syntax You can get the same effect with answered Jun 4, 2016 at 0:38
Nick MatteoNick Matteo 4,30721 silver badges31 bronze badges 0 these will both work in Python 2.7 and Python 3.x:
btw, answered Jun 4, 2016 at 0:56
Corey GoldbergCorey Goldberg 57.1k27 gold badges123 silver badges141 bronze badges 2 Try using join on a str conversion of your ints:
For python 3.7
answered Jun 4, 2016 at 0:38
EoinSEoinS 5,1751 gold badge18 silver badges31 bronze badges 2 You have multiple options, each with different general use cases. The first would be to use a
You could also use
My favorite method, however, would be to pass
answered Jun 4, 2016 at 1:45
2Cubed2Cubed 3,2216 gold badges22 silver badges40 bronze badges 1 If you write
You get this output:
answered Nov 30, 2019 at 13:40
Suraj Rao 29.1k11 gold badges95 silver badges100 bronze badges answered Mar 4, 2019 at 14:57
1
don't forget to put * before the List answered Apr 7 at 0:57
For python 2.7 another trick is:
answered Feb 7, 2019 at 4:30
elad silverelad silver 8,5294 gold badges41 silver badges63 bronze badges you can use more elements "end" in print:
answered Sep 25, 2020 at 10:03
Maybe this code will help you.
you can use an empty list variable to collect the user input, with method append(). and if you want to print list in one line you can use answered Jul 3, 2020 at 3:53
montimonti 1821 silver badge8 bronze badges How do I print an array of elements in one line?Print Array in One Line with Java Streams
Arrays. toString() and Arrays. toDeepString() just print the contents in a fixed manner and were added as convenience methods that remove the need for you to create a manual for loop.
How do you enter an array in one line in Python?Answer: Use map() function along with input and split function. Using int to Read an array of integers only.
How do you print all values on one line in Python?To print on the same line in Python, add a second argument, end=' ', to the print() function call.
How do I print all values on one line?Modify print() method to print on the same line
The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.
|