Konverter heltall til streng i Python
I Python kan et heltall konverteres til en streng ved hjelp av den innebygde str() funksjon. Str()-funksjonen tar inn en hvilken som helst str() er ikke den eneste måten å gjøre det på. Denne typen konvertering kan også gjøres ved å bruke %s nøkkelord, den .format funksjon eller bruk f-streng funksjon.
Nedenfor er listen over mulige måter å konvertere et heltall til streng i python:
1. Bruke str() funksjonen
Syntaks: str(heltallsverdi)
Eksempel:
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))> |
Produksjon:
Type of variable before conversion : Type After conversion :
2. Bruke %s nøkkelord
Syntaks: %s % heltall
Eksempel:
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))> |
Produksjon:
Type of variable before conversion : Type after conversion :
3. Bruke .format()-funksjonen
Syntaks: '{}'.format(heltall)
Eksempel:
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))> |
Produksjon:
Type before conversion : Type after conversion :
4. Bruke f-streng
Syntaks: f'{integer}'
Eksempel:
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))> |
Produksjon:
Type before conversion : Type after conversion :
5. Bruke __str__() metoden
Syntaks: I heltall.__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))> |
Produksjon:
Type before conversion : Type after conversion :
6. Bruke 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))> |
Produksjon
1234
7.Bruke join()-metoden:
join()-metoden brukes til å konvertere en liste over heltall til en streng. Vi konverterer heltallet til en liste med tegn ved å bruke list()-funksjonen og kobler dem deretter med join()-metoden.
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))> |
Produksjon
Type before conversion : Type after conversion :
Tidskompleksitet: O(N) hvor n er antall sifre i heltallet.
Romkompleksitet:O(N) som vi må lage en liste over tegn som har n elementer,