Home > other >  How to print, lets say you have an input 35, then your output should be 30 5. Or 456, output shoul
How to print, lets say you have an input 35, then your output should be 30 5. Or 456, output shoul

Time:10-08

I have tried this:

def place(n):
total = 1
value = 0
list1 = []
while (n != 0):
    rem = n % 10
    n = n//10
    value = total * rem
    list1.append(value)
    total = total * 10
print(list1)


n = int(input())
print(place(n))

But I am not sure how do I print the desired output iterating through the list.

CodePudding user response:

Use str.join to merge all of the values into a single string after reversing the order and converting each value to a string. You can use the map function to convert each of the values into a string and you can reverse a list by slicing it in reverse.

so...

list1 = [9, 50, 800]
list1 = list1[::-1]           # [800,50,9]
list1 = map(str, list1)       # ~ ["800", "50", "9"] 
list1 = "   ".join(list1)     # "800   50   9"

" ".join(map(str,list1[::-1]))

for example:

def place(n):
    total = 1
    value = 0
    list1 = []
    while (n != 0):
        rem = n % 10
        n = n//10
        value = total * rem
        list1.append(value)
        total = total * 10
    return "   ".join(map(str,list1[::-1]))

n = int(input())
print(place(n))
  • Related