Converti un numero intero in una stringa in Python

In Python un numero intero può essere convertito in una stringa utilizzando il metodo integrato str() funzione. La funzione str() accetta any str() non è l'unico modo per farlo. Questo tipo di conversione può essere eseguita anche utilizzando il file %S parola chiave, il .formato funzione o utilizzo corda F funzione.

Di seguito è riportato l'elenco dei modi possibili per convertire un numero intero in una stringa in Python:

1. Utilizzando la funzione str()

Sintassi: str(valore_intero)

Esempio:

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))>

Produzione:

Type of variable before conversion : Type After conversion : 

2. Utilizzando la parola chiave %s

Sintassi: %s % intero

Esempio:

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))>

Produzione:

Type of variable before conversion : Type after conversion : 

3. Utilizzo della funzione .format()

Sintassi: ‘{}’.format(intero)

Esempio:

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))>

Produzione:

Type before conversion : Type after conversion : 

4. Utilizzo della stringa F

Sintassi: f'{intero}’

Esempio:

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))>

Produzione:

Type before conversion : Type after conversion : 

5. Utilizzo del metodo __str__()

Sintassi: I intero.__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))>

Produzione:

Type before conversion : Type after conversion : 

6. Utilizzando 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))>

Produzione

1234 

7.Utilizzo del metodo join():

Il metodo join() viene utilizzato per convertire un elenco di numeri interi in una stringa. Convertiamo l'intero in un elenco di caratteri utilizzando la funzione list() e poi li uniamo utilizzando il metodo 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))>

Produzione

Type before conversion : Type after conversion : 

Complessità temporale: O(N) dove n è il numero di cifre dell'intero.

Complessità spaziale:O(N) COME dobbiamo creare un elenco di caratteri che abbia n elementi,