Python Forum
Need help getting the output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help getting the output
#9
As always there are different ways to achieve desired result. Following snippet is using built in string method str.zfill() combined with for loop and slicing:

>>> user_input = '5'                                               
>>> num = ''.join(str(x) for x in range(1, int(user_input) + 1))  # results '12345'
>>> for x in reversed(num):                                       # in reverse order 5->4->3->2->1
...     print(num[:int(x)].zfill(len(num)))                       # slice padded with zeros 
...
12345
01234
00123
00012
00001
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Need help getting the output - by aankrose - Apr-05-2019, 04:13 PM
RE: Need help getting the output - by Gribouillis - Apr-05-2019, 05:48 PM
RE: Need help getting the output - by Yoriz - Apr-05-2019, 06:19 PM
RE: Need help getting the output - by skorost5 - Apr-05-2019, 06:32 PM
RE: Need help getting the output - by aankrose - Apr-05-2019, 08:26 PM
RE: Need help getting the output - by Yoriz - Apr-05-2019, 08:41 PM
RE: Need help getting the output - by skorost5 - Apr-06-2019, 06:05 AM
RE: Need help getting the output - by aankrose - Apr-08-2019, 04:18 PM
RE: Need help getting the output - by perfringo - Apr-09-2019, 06:18 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020