Pretvori celo število v niz v Pythonu
V Pythonu je mogoče celo število pretvoriti v niz z uporabo vgrajenega str() funkcijo. Funkcija str() sprejme vse str() ni edini način za to. To vrsto pretvorbe lahko izvedete tudi z uporabo %s ključna beseda, the .format funkcijo ali uporabo f-niz funkcijo.
Spodaj je seznam možnih načinov za pretvorbo celega števila v niz v pythonu:
1. Uporaba funkcije str().
Sintaksa: str(celo število_vrednost)
primer:
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))> |
Izhod:
Type of variable before conversion : Type After conversion :
2. Uporaba ključne besede %s
Sintaksa: %s % celo število
primer:
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))> |
Izhod:
Type of variable before conversion : Type after conversion :
3. Uporaba funkcije .format().
Sintaksa: ‘{}’.format(celo število)
primer:
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))> |
Izhod:
Type before conversion : Type after conversion :
4. Uporaba f-niza
Sintaksa: f'{celo število}'
primer:
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))> |
Izhod:
Type before conversion : Type after conversion :
5. Uporaba metode __str__().
Sintaksa: I celo število.__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))> |
Izhod:
Type before conversion : Type after conversion :
6. Uporaba 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))> |
Izhod
1234
7. Uporaba metode join():
metoda join() se uporablja za pretvorbo seznama celih števil v niz. Celo število pretvorimo v seznam znakov s funkcijo list() in jih nato združimo z metodo 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))> |
Izhod
Type before conversion : Type after conversion :
Časovna zahtevnost: O(N) kjer je n število števk v celem številu.
Prostorska kompleksnost: O(N) kot ustvariti moramo seznam znakov, ki ima n elementov,