Previesť celé číslo na reťazec v Pythone
V Pythone je možné celé číslo previesť na reťazec pomocou vstavaného str() funkciu. Funkcia str() zaberá ľubovoľné str() nie je to jediný spôsob, ako to urobiť. Tento typ konverzie je možné vykonať aj pomocou %s kľúčové slovo, .formát funkciu alebo používanie f-string funkciu.
Nižšie je uvedený zoznam možných spôsobov, ako previesť celé číslo na reťazec v pythone:
1. Použitie funkcie str().
Syntax: str(celé_číslo)
Príklad:
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))> |
Výkon:
Type of variable before conversion : Type After conversion :
2. Pomocou kľúčového slova %s
Syntax: %s % celé číslo
Príklad:
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))> |
Výkon:
Type of variable before conversion : Type after conversion :
3. Použitie funkcie .format().
Syntax: ‘{}’.format(celé číslo)
Príklad:
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))> |
Výkon:
Type before conversion : Type after conversion :
4. Pomocou f-stringu
Syntax: f'{integer}'
Príklad:
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))> |
Výkon:
Type before conversion : Type after conversion :
5. Použitie metódy __str__().
Syntax: I celé číslo.__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))> |
Výkon:
Type before conversion : Type after conversion :
6. Použitie 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))> |
Výkon
1234
7. Použitie metódy join():
Metóda join() sa používa na konverziu zoznamu celých čísel na reťazec. Celé číslo prevedieme na zoznam znakov pomocou funkcie list() a následne ich spojíme pomocou metódy 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))> |
Výkon
Type before conversion : Type after conversion :
Časová zložitosť: O(N) kde n je počet číslic v celom čísle.
Priestorová zložitosť:O(N) ako musíme vytvoriť zoznam znakov, ktorý má n prvkov,