I Python kan ett heltal konverteras till en sträng med den inbyggda str() fungera. Str()-funktionen tar in vilken som helst str() är inte det enda sättet att göra det. Denna typ av konvertering kan också göras med hjälp av %s nyckelord, den .formatera funktion eller använder f-sträng fungera.
Nedan är listan över möjliga sätt att konvertera ett heltal till sträng i python:
1. Använd str()-funktionen
Syntax: str(heltalsvärde)
Exempel:
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))> |
>
>
Produktion:
Type of variable before conversion : Type After conversion :>
2. Använder nyckelordet %s
Syntax: %s % heltal
Exempel:
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))> |
>
>
Produktion:
Type of variable before conversion : Type after conversion :>
3. Använd funktionen .format().
Syntax: '{}'.format(heltal)
Exempel:
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))> |
>
>
Produktion:
Type before conversion : Type after conversion :>
4. Använd f-sträng
Syntax: f'{heltal}'
Exempel:
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))> |
>
>
Produktion:
Type before conversion : Type after conversion :>
5. Använder metoden __str__().
Syntax: I heltal.__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))> |
>
>
Produktion:
Type before conversion : Type after conversion :>
6. Använd str.isdigit()
Python3
Algoritm för bfs
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))> |
>
>Produktion
1234>
7.Använda join()-metoden:
join()-metoden används för att konvertera en lista med heltal till en sträng. Vi konverterar heltal till en lista med tecken med funktionen list() och sammanfogar dem sedan med metoden 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))> |
>
>Produktion
Type before conversion : Type after conversion :>
Tidskomplexitet: O(N) där n är antalet siffror i heltal.
Rymdkomplexitet:O(N) som vi måste skapa en lista med tecken som har n element,