Python Forum
Printing digits after the decimal point one by one
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing digits after the decimal point one by one
#1
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.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  method to remove zero digits from fraction in decimal Skaperen 17 2,573 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  Single digits seem to be greater than double digits Frosty_GM 4 3,436 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,525 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  testing for Decimal w/o importing decimal every time Skaperen 7 4,363 May-06-2019, 10:23 PM
Last Post: Skaperen
  decimal point or comma Skaperen 8 5,143 Mar-17-2019, 06:02 AM
Last Post: DeaD_EyE
  mixed decimal point characters Skaperen 1 2,158 Mar-10-2019, 05:23 PM
Last Post: stullis
  I'm dividing large numbers of about 25 million digits I need to retain decimal format Pleiades 2 3,142 Apr-26-2018, 07:50 AM
Last Post: Pleiades
  Add parameter to math.floor() to round to specific decimal point gabrield 2 2,675 Mar-09-2018, 08:43 PM
Last Post: wavic
  decimal or hexadecimal digits to int Skaperen 3 4,095 Sep-26-2017, 07:03 AM
Last Post: Skaperen
  Input a number with any number of digits and output it in reverse order of digits. sarada2099 6 6,574 Mar-12-2017, 05:45 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