Konvertuokite sveikąjį skaičių į eilutę „Python“.
„Python“ programoje sveikasis skaičius gali būti konvertuojamas į eilutę naudojant integruotą str() funkcija. Funkcija str() paima bet kurią str() nėra vienintelis būdas tai padaryti. Šio tipo konvertavimą taip pat galima atlikti naudojant %s raktinis žodis, .formatas funkcija arba naudojimas f eilutė funkcija.
Žemiau pateikiamas galimų būdų, kaip konvertuoti sveikąjį skaičių į eilutę python, sąrašas:
1. Naudojant str() funkciją
Sintaksė: str(sveikasis skaičius_vertė)
Pavyzdys:
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))> |
Išvestis:
Type of variable before conversion : Type After conversion :
2. Naudojant %s raktinį žodį
Sintaksė: %s % sveikasis skaičius
Pavyzdys:
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))> |
Išvestis:
Type of variable before conversion : Type after conversion :
3. Funkcijos .format() naudojimas
Sintaksė: '{}'.format(integer)
Pavyzdys:
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))> |
Išvestis:
Type before conversion : Type after conversion :
4. F-stygos naudojimas
Sintaksė: f'{integer}
Pavyzdys:
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))> |
Išvestis:
Type before conversion : Type after conversion :
5. Naudojant __str__() metodą
Sintaksė: I sveikasis skaičius.__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))> |
Išvestis:
Type before conversion : Type after conversion :
6. Naudojant 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))> |
Išvestis
1234
7. Join() metodo naudojimas:
Join() metodas naudojamas sveikųjų skaičių sąrašui konvertuoti į eilutę. Mes konvertuojame sveikąjį skaičių į simbolių sąrašą naudodami funkciją list() ir sujungiame juos naudodami prisijungimo() metodą.
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))> |
Išvestis
Type before conversion : Type after conversion :
Laiko sudėtingumas: O(N) kur n yra sveikojo skaičiaus skaitmenų skaičius.
Erdvės sudėtingumas: O (N) kaip turime sukurti simbolių sąrašą, kuriame yra n elementų,