how to turn the list into a single string

Asked

Viewed 42 times

-3

I would like my code to result instead of a list a single string Ex: n = 5 the code returns me [1, 2, 3, 4, 5] I would like it to return me '12345' transforming all elements into a single string. what can I do?

n = int(input())

previous = []
i = 1

while(i <= n):
    previous.append(i)
    i = i + 1
    if(len(previous) == n):
            print(previous)
  • Replace all your code with that line print("".join(map(str,range(1,int(input())+1))))

1 answer

-1


You can use the method join, converting the items to string before and concatenating them from an empty string '':

print(''.join(str(x) for x in previous))

With the input [1, 2, 3, 4, 5] the result would be:

12345
  • thank you worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.