i want to display this number pattern in python, help me!

9
89
789
6789
56789
456789
3456789
23456789
123456789

Recommended Answers

All 5 Replies

Python has a nice thing called sequence slicing:
[starting-at-index : but-less-than-index [ : step]]
'start' defaults to 0, 'end' to len(sequence), 'step' to 1

for instance
s = "123456789"
then
s[-1:] --> 9
s[-2:] --> 89
s[-3:] --> 789
and so on,
do this with a for loop in range(1, 10)

s = "123456789"

for k in range(1, 10):
    print(s[-k:])

''' output...
9
89
789
6789
56789
456789
3456789
23456789
123456789
'''

@vegaseat - As a long standing member you should know better than to reward lazy/bad behaviour.

You are so right, I thought since 2 month had gone since the original request the whole thing became a teaser for the mind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.