Convertir un entier en chaîne en Python

En Python, un entier peut être converti en chaîne à l'aide de la fonction intégrée str() fonction. La fonction str() prend en compte n'importe quel str() n'est pas le seul moyen de le faire. Ce type de conversion peut également être effectué à l'aide du %s mot-clé, le .format fonction ou en utilisant f-string fonction.

Vous trouverez ci-dessous la liste des méthodes possibles pour convertir un entier en chaîne en python :

1. Utilisation de la fonction str()

Syntaxe: str(valeur_entière)

Exemple:

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

Sortir:

Type of variable before conversion : Type After conversion : 

2. Utilisation du mot-clé %s

Syntaxe: %s % entier

Exemple:

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

Sortir:

Type of variable before conversion : Type after conversion : 

3. Utilisation de la fonction .format()

Syntaxe: '{}'.format (entier)

Exemple:

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

Sortir:

Type before conversion : Type after conversion : 

4. Utiliser la f-string

Syntaxe: f'{entier}'

Exemple:

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

Sortir:

Type before conversion : Type after conversion : 

5. Utilisation de la méthode __str__()

Syntaxe : je entier.__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))>

Sortir:

Type before conversion : Type after conversion : 

6. Utilisation de 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))>

Sortir

1234 

7.Utilisation de la méthode join() :

La méthode join() est utilisée pour convertir une liste d’entiers en chaîne. Nous convertissons l'entier en une liste de caractères à l'aide de la fonction list(), puis les joignons à l'aide de la méthode 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))>

Sortir

Type before conversion : Type after conversion : 

Complexité temporelle : O(N) où n est le nombre de chiffres de l'entier.

Complexité spatiale : O(N) comme nous devons créer une liste de caractères comportant n éléments,