Python Forum
print on a single line with start/end brackets and commas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print on a single line with start/end brackets and commas
#1
Hello,
Does anybody know how to print the results of the code below on a single line with square brackets at the beginning and at the end and the single results separated by a comma (except after the last result)? Practically I need to get this:
[100, 400, 900, 1600]
With my coding I get this:
100 400 900 1600
Tried different ways, but I'm not getting the output in the correct format.

numbers=[10, 20, 30, 40]
def square_values(numbers):
    return numbers**2
for current_number in numbers:
    current_square=square_values(current_number)
    print(f'{current_square} ',end='')
Reply
#2
result = []
for current_number in numbers:
    result.append(square_values(current_number))
print(result)
Output:
[100, 400, 900, 1600]
Paulman likes this post
Reply
#3
That's great, many thanks!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad Check if a given string has its open brackets closed by the same type of brackets? noahverner1995 1 1,236 Apr-11-2022, 02:13 PM
Last Post: deanhystad
  Reading a text until matched string and print it as a single line cananb 1 2,017 Nov-29-2020, 01:38 PM
Last Post: DPaul
  String formatting - tax brackets darek88 1 1,992 Aug-28-2020, 09:59 AM
Last Post: Larz60+
  Analyzing data seperated by commas Kappel 2 1,895 Aug-07-2019, 01:46 PM
Last Post: ThomasL
  Regex: Matches a number with commas for every three digits linh_py 2 3,746 Jun-25-2019, 03:47 AM
Last Post: linh_py
  How to force print statement to print on one line wlsa 4 3,534 Oct-28-2018, 09:39 PM
Last Post: wavic
  how can i print two value in single line desul 8 6,126 Feb-28-2017, 06:09 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020