Muunna kokonaisluku merkkijonoksi Pythonissa
Pythonissa kokonaisluku voidaan muuntaa merkkijonoksi sisäänrakennetun ohjelman avulla str() toiminto. Str()-funktio ottaa minkä tahansa str() ei ole ainoa tapa tehdä niin. Tämän tyyppinen muunnos voidaan tehdä myös käyttämällä %s avainsana, .muoto toimintoa tai käyttöä f-merkkijono toiminto.
Alla on luettelo mahdollisista tavoista muuntaa kokonaisluku merkkijonoksi pythonissa:
1. Str()-funktion käyttäminen
Syntaksi: str(kokonaisluku_arvo)
Esimerkki:
Python 3
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))> |
Lähtö:
Type of variable before conversion : Type After conversion :
2. Käytä avainsanaa %s
Syntaksi: %s % kokonaisluku
Esimerkki:
Python 3
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))> |
Lähtö:
Type of variable before conversion : Type after conversion :
3. Käytä .format()-funktiota
Syntaksi: '{}'.format(integer)
Esimerkki:
Python 3
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))> |
Lähtö:
Type before conversion : Type after conversion :
4. F-merkkijono
Syntaksi: f'{kokonaisluku}'
Esimerkki:
Python 3
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))> |
Lähtö:
Type before conversion : Type after conversion :
5. Käytä menetelmää __str__().
Syntaksi: I kokonaisluku.__str__()
Python 3
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))> |
Lähtö:
Type before conversion : Type after conversion :
6. Str.isdigit()
Python 3
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))> |
Lähtö
1234
7. Join()-menetelmän käyttäminen:
join()-menetelmää käytetään muuttamaan kokonaislukuluettelo merkkijonoksi. Muunnamme kokonaisluvun merkkiluetteloksi lista()-funktiolla ja yhdistämme ne sitten join()-menetelmällä.
Python 3
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))> |
Lähtö
Type before conversion : Type after conversion :
Aika monimutkaisuus: O(N) missä n on kokonaisluvun numeroiden lukumäärä.
Tilan monimutkaisuus: O(N) kuten meidän on luotava luettelo hahmoista, joissa on n elementtiä,