Friday, 28 October 2022

Reverse number in Python using string slicing


num = 654321
print(str(num)[::-1])

Output

654321

Using the string slicing concept, you can get reverse the string. ::-1 corresponds to start:stop:step. When you pass -1 as step, the start point goes to the end and stop at the front.


Source : https://www.programiz.com/python-programming/examples/reverse-a-number


 

No comments:

Post a Comment