How to convert an int number to string in python?

I need to do an exercise in which I input a 3-digit integer variable and python has to show it inverted, but up to where I know an integer variable n can do that, but a string does. What can I do?

Author: Rodrigo Costa, 2017-03-23

5 answers

You can use:

str(10);

And from string to int you can use:

int('10');
 7
Author: David Melo, 2017-03-23 18:26:50

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

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

Demo

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

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

As mentioned in other answers.

And to have this reversal again in integer:

int(str(123)[::-1]) # 321
 5
Author: Miguel, 2017-03-24 17:14:57

When you say

I input 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 always being a string. Thus, inverting the number would be inverting the string. If you are using Python 2, the input function will return a number itself, but you could use raw_input, which returns a string.

Documentation:


Interpreting as a function in which a number will be sent, you can convert to string as per David's response Melo .


Interpreting how to do this inversion using only mathematical operations, you can resort to modulus and integer 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)
 4
Author: Jefferson Quesado, 2017-04-13 12:59:44

To solve this question it is enough:

  1. capture numeric value as string;
  2. revert 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 execute this code we receive the following message: Digite um número com 3 dígitos: . At this point we must enter an integer with 3 digits and press enter.

From this moment on input() will capture the typed value, revert the order of the digits and then will display the reversed value.

 0
Author: Solkarped, 2020-10-18 01:26:25

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 logarithm of base 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 raised to power p (this will "move" the number that we want at position p for the end of the number);
  • get the rest of dividing that number by 10 (this will isolate the last number);
  • multiply this remainder by 10 raised to 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 will 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
 0
Author: Fábio Batista, 2020-10-18 16:38:51