Převeďte celé číslo na řetězec v Pythonu

V Pythonu lze celé číslo převést na řetězec pomocí vestavěného str() funkce. Funkce str() zabere libovolné str() není jediný způsob, jak toho dosáhnout. Tento typ převodu lze také provést pomocí %s klíčové slovo, .formát funkce nebo používání f-string funkce.

Níže je uveden seznam možných způsobů, jak převést celé číslo na řetězec v pythonu:

1. Použití funkce str().

Syntax: str(celočíselná_hodnota)

Pří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ýstup:

Type of variable before conversion : Type After conversion : 

2. Pomocí klíčového slova %s

Syntax: %s % celé číslo

Pří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ýstup:

Type of variable before conversion : Type after conversion : 

3. Použití funkce .format().

Syntax: ‚{}‘.format(celé číslo)

Pří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ýstup:

Type before conversion : Type after conversion : 

4. Použití f-string

Syntax: f'{integer}'

Pří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ýstup:

Type before conversion : Type after conversion : 

5. Použití metody __str__().

Syntaxe: 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ýstup:

Type before conversion : Type after conversion : 

6. Použití 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ýstup

1234 

7. Použití metody join():

Metoda join() se používá k převodu seznamu celých čísel na řetězec. Celé číslo převedeme na seznam znaků pomocí funkce list() a poté je spojíme pomocí metody 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ýstup

Type before conversion : Type after conversion : 

Časová složitost: O(N) kde n je počet číslic v celém čísle.

Vesmírná složitost:O(N) tak jako musíme vytvořit seznam znaků, který má n prvků,