Python Forum
Printing digits after the decimal point one by one - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Printing digits after the decimal point one by one (/thread-30417.html)



Printing digits after the decimal point one by one - uberman321 - Oct-20-2020

I recently began learning Python and I am trying to write a program in which I print the digits after the decimal point one by one once the user enters a non-integer value.

So far I have this:

for x in str(num).split(".")[1]:
print(int(x))

With this, if num = 0.089 as an example, it would give me:

0

8

9

However, I want it like this:

9

8

0

How should I change my code? Thanks.


RE: Printing digits after the decimal point one by one - bowlofred - Oct-20-2020

You could wrap your split-off string in a reversed(), or you could iterate over it backward.

>>> for x in reversed(str(0.089).split(".")[1]): print(x)
...
9
8
0
>>> for x in str(0.089).split(".")[1][::-1]: print(x)
...
9
8
0