0

How can I solve this assignment:

Given string in form "3,9,13,4,42" convert to list and for each element calculate his square. Then join that squares to string and write to console. Input

`string = "3,9,13,4,42"`

Output

`string = "9,81,169,16,1764"`

I tryed this:

string = "3,9,13,4,42"
l = [int(i) for i in string.split(',')]
for i in l:
    i = i**2

Now I have all squares but how to join them in string?

masterdeki
  • 49
  • 6
  • 3
    `','.join([str(int(i)**2) for i in string.split(',')])`. Incidentally, do not use reserved or builtin names such as `list`, etc... – keepAlive Jan 05 '21 at 01:19
  • 3
    Does this answer your question? [Join a list of items with different types as string in Python](https://stackoverflow.com/questions/3590165/join-a-list-of-items-with-different-types-as-string-in-python) – Kacperito Jan 05 '21 at 01:21
  • 1
    @keepAlive For reference: [TypeError: 'list' object is not callable in python](https://stackoverflow.com/q/31087111/4518341) – wjandrea Jan 05 '21 at 01:24

0 Answers0