Convertir un número entero a una cadena en Python

En Python, un número entero se puede convertir en una cadena usando el método incorporado cadena() función. La función str() toma cualquier cadena() no es la única manera de hacerlo. Este tipo de conversión también se puede realizar utilizando el %s palabra clave, el .formato función o uso cuerda f función.

A continuación se muestra la lista de posibles formas de convertir un número entero en una cadena en Python:

1. Usando la función str()

Sintaxis: str(valor_entero)

Ejemplo:

Python3




num> => 10> # check and print type of num variable> print> (> 'Type of variable before conversion : '> ,> type> (num))> # convert the num into string> converted_num> => str> (num)> # check and print type converted_num variable> print> (> 'Type After conversion : '> ,> type> (converted_num))>

Producción:

Type of variable before conversion : Type After conversion : 

2. Usando %s palabra clave

Sintaxis: %s % entero

Ejemplo:

Python3




num> => 10> # check and print type of num variable> print> (> 'Type of variable before conversion : '> ,> type> (num))> # convert the num into string and print> converted_num> => '% s'> %> num> print> (> 'Type after conversion : '> ,> type> (converted_num))>

Producción:

Type of variable before conversion : Type after conversion : 

3. Usando la función .format()

Sintaxis: '{}'.formato(entero)

Ejemplo:

Python3




num> => 10> # check and print type of num variable> print> (> 'Type before conversion : '> ,> type> (num))> # convert the num into string and print> converted_num> => '{}'> .> format> (num)> print> (> 'Type after conversion :'> ,> type> (converted_num))>

Producción:

Type before conversion : Type after conversion : 

4. Usando cuerda f

Sintaxis: f'{entero}’

Ejemplo:

Python3




num> => 10> # check and print type of num variable> print> (> 'Type before conversion : '> ,> type> (num))> # convert the num into string> converted_num> => f> '{num}'> # print type of converted_num> print> (> 'Type after conversion : '> ,> type> (converted_num))>

Producción:

Type before conversion : Type after conversion : 

5. Usando el método __str__()

Sintaxis: yo entero.__str__()

Python3




num> => 10> # check and print type of num variable> print> (> 'Type before conversion : '> ,> type> (num))> # convert the num into string> converted_num> => num.__str__()> # print type of converted_num> print> (> 'Type after conversion : '> ,> type> (converted_num))>

Producción:

Type before conversion : Type after conversion : 

6. Usando str.isdigit()

Python3




str_value> => '1234'> if> str_value.isdigit():> > int_value> => int> (str_value)> > print> (int_value)> > print> (> type> (int_value))> else> :> > raise> ValueError(> 'Invalid literal for int(): {}'> .> format> (str_value))>

Producción

1234 

7.Usando el método join():

El método join() se utiliza para convertir una lista de números enteros en una cadena. Convertimos el número entero en una lista de caracteres usando la función list() y luego los unimos usando el método join().

Python3




num> => 10> # check and print type of num variable> print> (> 'Type before conversion : '> ,> type> (num))> # convert the num into string> converted_num> => ''.join(> list> (> str> (num)))> # print type of converted_num> print> (> 'Type after conversion : '> ,> type> (converted_num))>

Producción

Type before conversion : Type after conversion : 

Complejidad del tiempo: O(N) donde n es el número de dígitos del número entero.

Complejidad espacial:O(N) como Necesitamos crear una lista de personajes que tenga n elementos.