Converteix nombre enter en cadena en Python

A Python, un nombre enter es pot convertir en una cadena utilitzant el sistema integrat str() funció. La funció str() admet qualsevol str() no és l'única manera de fer-ho. Aquest tipus de conversió també es pot fer mitjançant el %s paraula clau, la .format funció o ús corda f funció.

A continuació es mostra la llista de possibles maneres de convertir un nombre enter en cadena en Python:

1. Utilitzant la funció str().

Sintaxi: str(valor_enter)

Exemple:

Python 3




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

Sortida:

Type of variable before conversion : Type After conversion : 

2. Utilitzant la paraula clau %s

Sintaxi: %s % enter

Exemple:

Python 3




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

Sortida:

Type of variable before conversion : Type after conversion : 

3. Utilitzant la funció .format().

Sintaxi: '{}'.format(enter)

Exemple:

Python 3




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

Sortida:

Type before conversion : Type after conversion : 

4. Utilitzant la cadena f

Sintaxi: f'{enter}'

Exemple:

Python 3




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

Sortida:

Type before conversion : Type after conversion : 

5. Utilitzant el mètode __str__().

Sintaxi: I enter.__str__()

Python 3




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

Sortida:

Type before conversion : Type after conversion : 

6. Utilitzant str.isdigit()

Python 3




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

Sortida

1234 

7. Ús del mètode join():

El mètode join() s'utilitza per convertir una llista d'enters en una cadena. Convertim l'enter a una llista de caràcters mitjançant la funció list() i després els unim mitjançant el mètode join().

Python 3




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

Sortida

Type before conversion : Type after conversion : 

Complexitat temporal: O(N) on n és el nombre de dígits del nombre enter.

Complexitat espacial: O(N) com hem de crear una llista de caràcters que tingui n elements,