У Python ціле число можна перетворити на рядок за допомогою вбудованої функції str() функція. Функція str() приймає будь-які str() це не єдиний спосіб зробити це. Цей тип перетворення також можна виконати за допомогою %s ключове слово, the .формат функція або використання f-рядок функція.
Нижче наведено список можливих способів перетворення цілого числа на рядок у python:
1. Використання функції str().
Синтаксис: str(ціле_значення)
приклад:
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))> |
>
>
Вихід:
Type of variable before conversion : Type After conversion :>
2. Використання ключового слова %s
Синтаксис: %s % ціле число
приклад:
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))> |
>
>
Вихід:
Type of variable before conversion : Type after conversion :>
3. Використання функції .format().
Синтаксис: ‘{}’.format(ціле число)
приклад:
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))> |
>
>
Вихід:
Type before conversion : Type after conversion :>
4. Використання f-рядка
Синтаксис: f'{ціле число}'
приклад:
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))> |
>
>
Вихід:
Type before conversion : Type after conversion :>
5. Використання методу __str__().
Синтаксис: І integer.__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))> |
>
>
Вихід:
Type before conversion : Type after conversion :>
6. Використання str.isdigit()
Python3
зберегти відео youtube vlc
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))> |
>
>Вихід
1234>
7. Використання методу join():
Метод join() використовується для перетворення списку цілих чисел у рядок. Ми перетворюємо ціле число на список символів за допомогою функції list(), а потім об’єднуємо їх за допомогою методу 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))> |
>
>Вихід
Type before conversion : Type after conversion :>
Часова складність: O(N) де n – кількість цифр у цілому числі.
Просторова складність: O(N) як нам потрібно створити список символів, який має n елементів,