How to convert an int number to string in python?

Asked

Viewed 29,639 times

5

I need to do an exercise where I input an integer variable of 3 digits and the python has to show it reversed, but up to where I know an integer variable n can do this, but a string can. What can I do?

5 answers

7


You can use:

str(10);

And from string to int you can use:

int('10');

5

To reverse the string that returns from input(), which already returns a string and not an integer, you can do so:

num = input("introduza um num") # '123'
revertido = num[::-1]
print(revertido) # '321'

DEMONSTRATION

But to turn an integer into a string and reverse it would be:

str(123)[::-1] # '321'

As mentioned in other answers.

And to get that reversal back in one piece:

int(str(123)[::-1]) # 321

4

When you say

i inputo a 3 digit integer variable

Interpreted as

i read from the standard input an integer

If you are using Python 3, you read from the default input with the function input, the return being always a string. Thus, reversing the number would reverse the string. If you are using Python 2, the function input will return a number even, but you could use the raw_input, that returns a string.

Documentation:


By interpreting it as a function in which a number will be sent, you can convert to string according to the reply from David Melo.


By interpreting how to do this inversion using only mathematical operations, you can turn to the entire module and division.

For example, in Python 3:

a = 123 # número a inverter
b = 0 # número invertido

for i in range(3):
    # pegue o último dígito de 'a' e coloque após os valores atuais de 'b'
    b = b*10 + (a % 10)
    # remova o último dígito de 'a'
    a = a//10
print(b)

0

To resolve this issue is enough:

  1. Capture numeric value as string;
  2. Reverse this string;
  3. Display the reverted value.

With this logic we can implement the following code:

print(input('Digite um número com 3 dígitos: ')[::-1])

When we executed this code we received the following message: Digite um número com 3 dígitos: . At this point we must enter an integer number with 3 digits and press enter.

From this moment on input() capture the entered value, revert the order of the digits and then display the reverted value.

0

There is a mathematical way to invert a number, which works for any number of digits:

import math

def inverte(n):
    digitos = int(math.ceil(math.log10(n)))
    return sum(n / 10**p % 10 * 10**(digitos-p-1) for p in range(digitos))

First we need to know the number of digits of a number. The base logarithm 10 tells us this, when rounded up.

Then we need to run the following algorithm, once per position p (a 3-digit number will have positions 0, 1 and 2):

  • Divide the number by 10 high to power p (this will "move" the number we want in position p to the end of the number);
  • Get the rest of the division of that number by 10 (this will isolate the last number);
  • Multiply this remainder by 10 to the power digits-p-1 (this will put the number in the correct position in the final number)

Finally, we add up the numbers resulting from each iteration, and we have the number reversed.

An example of execution:

numero = 1234
digitos = 4
p0:
  1234 / (10 ** 0) = 1234 / 1 = 1234
  1234 % 10 = 4
  4 * (10 ** (4 - 0 - 1)) = 4 * (10 ** 3) = 4 * 1000 = 4000
p1:
  1234 / (10 ** 1) = 1234 / 10 = 123
  123 % 10 = 3
  3 * (10 ** (4 - 1 - 1)) = 3 * (10 ** 2) = 3 * 100 = 300
p2:
  1234 / (10 ** 2) = 1234 / 100 = 12
  12 % 10 = 2
  2 * (10 ** (4 - 2 - 1)) = 2 * (10 ** 1) = 2 * 10 = 20
p3:
  1234 / (10 ** 3) = 1234 / 1000 = 1
  1 % 10 = 1
  1 * (10 ** (4 - 3 - 1)) = 1 * (10 ** 0) = 1 * 1 = 1

soma = 4000 + 300 + 20 + 1 = 4321

Browser other questions tagged

You are not signed in. Login or sign up in order to post.