+ 1
How i can reverse 4 digit number in python???
32 Antworten
+ 2
num = 4808
text = str(num)
print(text[::-1])
+ 9
Show us your attempt whatever you have tried.
+ 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.
+ 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.
+ 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/
+ 6
n = input(" Enter four digit number: ")
n = n[::-1]
print(int(n))
+ 5
^_^ HonFu
+ 4
Your teacher thought you can do it - we trust in him/her. ;-)
+ 4
Homework finally finished. 🙄
+ 3
It can be even shorter.
Teachers may limit your choices to practice a specific part of the language.
+ 3
Isabel OK 👍
+ 3
Rohit Patwal write the following code.
n = input()
n = n[::-1]
print(n)
+ 2
if i were you i would count the number and read from backward.
+ 2
HonFu yes I checked it
There is solution but using while loop
And I need answer using if else statement...
+ 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.
+ 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.
+ 2
Isabel why did you add 1 to get the result for calc?
+ 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.
+ 1
Nothing I am just a beginner...
And I have to reverse the 4 digit number using if else...
Help me..
+ 1
Thank you so much...