Converteer geheel getal naar tekenreeks in Python

In Python kan een geheel getal worden omgezet in een string met behulp van de ingebouwde str() functie. De functie str() neemt elk bestand over str() is niet de enige manier om dit te doen. Dit type conversie kan ook worden uitgevoerd met behulp van de %S trefwoord, de .formaat functie of gebruik f-snaar functie.

Hieronder vindt u de lijst met mogelijke manieren om een ​​geheel getal naar een tekenreeks in Python te converteren:

1. De functie str() gebruiken

Syntaxis: str(geheel getal)

Voorbeeld:

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

Uitgang:

Type of variable before conversion : Type After conversion : 

2. Gebruik het trefwoord %s

Syntaxis: %s % geheel getal

Voorbeeld:

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

Uitgang:

Type of variable before conversion : Type after conversion : 

3. De functie .format() gebruiken

Syntaxis: ‘{}’.format(geheel getal)

Voorbeeld:

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

Uitgang:

Type before conversion : Type after conversion : 

4. F-snaar gebruiken

Syntaxis: f'{geheel getal}’

Voorbeeld:

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

Uitgang:

Type before conversion : Type after conversion : 

5. Gebruik de methode __str__().

Syntaxis: ik geheel getal.__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))>

Uitgang:

Type before conversion : Type after conversion : 

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

Uitvoer

1234 

7. Gebruik de join()-methode:

join() methode wordt gebruikt om een ​​lijst met gehele getallen naar een string te converteren. We converteren het gehele getal naar een lijst met tekens met behulp van de list()-functie en voegen deze vervolgens samen met behulp van de join()-methode.

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

Uitvoer

Type before conversion : Type after conversion : 

Tijdcomplexiteit: O(N) waarbij n het aantal cijfers van het gehele getal is.

Ruimtecomplexiteit: O (N) als we moeten een lijst met karakters maken die n elementen bevat,