Now I am looking for the best way to reverse the digits in an integer. For instance x = 12345 should become y = 54321.
sneekula 969
Nearly a Posting Maven
Recommended Answers
Jump to PostWhen I first looked at this problem, I thought it would be very simple routine stuff. To my surprise this thing is loaded with programming possibilities.
# reverse integer digit by digit, the long way ... x = 12345 x_string = str(x) # "12345" x_list = list(x_string) …
Jump to PostThank you! I am still trying to digest the one liners, particularly:
x_reversed = int(str(x)[::-1])
Can anybody break that down for me so I can see the flow?
x = 12345 x_string = str(x) # "12345" x_reversedstring = x_string[::-1] # "54321" x_reversed = int(x_reversedstring) # 54321
All 7 Replies
vegaseat 1,735
DaniWeb's Hypocrite Team Colleague
sneekula 969
Nearly a Posting Maven
Ene Uran 638
Posting Virtuoso
Mouche
vegaseat 1,735
DaniWeb's Hypocrite Team Colleague
jrcagle 77
Practically a Master Poster
ghostdog74 57
Junior Poster
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.