Pārvērst veselu skaitli par virkni Python

Programmā Python veselu skaitli var pārveidot par virkni, izmantojot iebūvēto str() funkciju. Funkcija str() uzņem jebkuru str() nav vienīgais veids, kā to izdarīt. Šāda veida konvertēšanu var veikt arī, izmantojot %s atslēgvārds, .formāts funkcija vai lietošana f-string funkciju.

Zemāk ir saraksts ar iespējamiem veidiem, kā pārvērst veselu skaitli par virkni programmā python:

1. Izmantojot str() funkciju

Sintakse: str(vesels_vērtība)

Piemērs:

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))>

Izvade:

Type of variable before conversion : Type After conversion : 

2. Atslēgvārda %s izmantošana

Sintakse: %s % vesels skaitlis

Piemērs:

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))>

Izvade:

Type of variable before conversion : Type after conversion : 

3. Funkcijas .format() izmantošana

Sintakse: '{}'.format(integer)

Piemērs:

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))>

Izvade:

Type before conversion : Type after conversion : 

4. Izmantojot f-string

Sintakse: f'{integer}

Piemērs:

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))>

Izvade:

Type before conversion : Type after conversion : 

5. Izmantojot __str__() metodi

Sintakse: I vesels skaitlis.__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))>

Izvade:

Type before conversion : Type after conversion : 

6. Izmantojot 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))>

Izvade

1234 

7. Izmantojot pievienošanās() metodi:

join() metode tiek izmantota, lai pārvērstu veselu skaitļu sarakstu virknē. Mēs pārvēršam veselo skaitli rakstzīmju sarakstā, izmantojot funkciju list(), un pēc tam savienojam tos, izmantojot join() metodi.

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))>

Izvade

Type before conversion : Type after conversion : 

Laika sarežģītība: O(N) kur n ir vesela skaitļa ciparu skaits.

Telpas sarežģītība: O(N) mums ir jāizveido rakstzīmju saraksts, kurā ir n elementi,