How i can reverse 4 digit number in python??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How i can reverse 4 digit number in python???

6th Feb 2020, 4:01 PM
Rohit Patwal
Rohit Patwal - avatar
32 Answers
+ 2
num = 4808 text = str(num) print(text[::-1])
8th Feb 2020, 2:04 PM
Joshua Evuetapha
Joshua Evuetapha - avatar
+ 9
Show us your attempt whatever you have tried.
6th Feb 2020, 4:03 PM
A͢J
A͢J - avatar
+ 8
Rohit Patwal Google has solutions of every problem so try to do some R&D there. If you are getting any problem after that then ask here.
6th Feb 2020, 4:45 PM
A͢J
A͢J - avatar
+ 7
azulejo I have not tried the method in the link because I don’t understand why it’s necessary. Would it be faster to do this? test = 12345 convert = str(test) reverse = convert[::-1] If you do print(reverse), you get 54321 as a string. But if you want to use it as an integer... calc = int(reverse) + 1 print(calc) gives you 54322.
7th Feb 2020, 12:47 AM
Isabel
Isabel - avatar
+ 6
I am a beginner, too, and I am learning that Google is your friend when there is something you can’t figure out how to do. I believe here is the answer you are looking for https://www.geeksforgeeks.org/write-a-program-to-reverse-digits-of-a-number/
6th Feb 2020, 4:25 PM
Isabel
Isabel - avatar
+ 6
n = input(" Enter four digit number: ") n = n[::-1] print(int(n))
7th Feb 2020, 10:42 PM
Duncan
Duncan - avatar
+ 5
^_^ HonFu
7th Feb 2020, 9:17 PM
Scooby
Scooby - avatar
+ 4
Your teacher thought you can do it - we trust in him/her. ;-)
6th Feb 2020, 4:19 PM
HonFu
HonFu - avatar
+ 4
Homework finally finished. 🙄
7th Feb 2020, 8:58 PM
HonFu
HonFu - avatar
+ 3
It can be even shorter. Teachers may limit your choices to practice a specific part of the language.
7th Feb 2020, 5:39 PM
HonFu
HonFu - avatar
+ 3
Isabel OK 👍
8th Feb 2020, 3:28 AM
Sonic
Sonic - avatar
+ 3
Rohit Patwal write the following code. n = input() n = n[::-1] print(n)
8th Feb 2020, 5:47 AM
Tanmay Gupta
Tanmay Gupta - avatar
+ 2
if i were you i would count the number and read from backward.
6th Feb 2020, 6:18 PM
\•/
\•/ - avatar
+ 2
HonFu yes I checked it There is solution but using while loop And I need answer using if else statement...
7th Feb 2020, 1:54 PM
Rohit Patwal
Rohit Patwal - avatar
+ 2
I also don't understand why it's necessary to use if...else. More simplier way: a=list(input()) a.reverse() print(int("".join(a))) That's all.
7th Feb 2020, 5:21 PM
Igor Olkhin
Igor Olkhin - avatar
+ 2
Basically two methods and their variations are possible: 1) use string representation of number, reverse the string and convert it back to an integer. 2) Use modulo and integer division (remainder and quotient when divided by 10) to get all digits from least to most significant of the original number to create a new number in a loop.
7th Feb 2020, 11:10 PM
Sonic
Sonic - avatar
+ 2
Isabel why did you add 1 to get the result for calc?
7th Feb 2020, 11:11 PM
Sonic
Sonic - avatar
+ 2
Sonic calc was only to show that after you use [::-1] to flip a number, you can turn it back into an integer and use it for calculations with other integers.
8th Feb 2020, 2:12 AM
Isabel
Isabel - avatar
+ 1
Nothing I am just a beginner... And I have to reverse the 4 digit number using if else... Help me..
6th Feb 2020, 4:05 PM
Rohit Patwal
Rohit Patwal - avatar
+ 1
Thank you so much...
6th Feb 2020, 4:27 PM
Rohit Patwal
Rohit Patwal - avatar