logo

Konwertuj liczbę całkowitą na ciąg w Pythonie

W Pythonie liczbę całkowitą można przekonwertować na ciąg znaków za pomocą wbudowanej funkcji str() funkcjonować. Funkcja str() przyjmuje dowolne str() to nie jedyny sposób, aby to zrobić. Ten typ konwersji można również wykonać za pomocą %S słowo kluczowe, .format funkcję lub użycie ciąg f funkcjonować.

Poniżej znajduje się lista możliwych sposobów konwersji liczby całkowitej na ciąg w Pythonie:

1. Korzystanie z funkcji str().



Składnia: str(wartość_całkowita)

Przykład:

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

>

>

Wyjście:

Type of variable before conversion : Type After conversion :>

2. Używanie słowa kluczowego %s

Składnia: %s% liczba całkowita

Przykład:

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

>

>

Wyjście:

Type of variable before conversion : Type after conversion :>

3. Korzystanie z funkcji .format().

Składnia: „{}”.format(liczba całkowita)

Przykład:

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

>

>

Wyjście:

Type before conversion : Type after conversion :>

4. Korzystanie z ciągu f

Składnia: f'{liczba całkowita}'

Przykład:

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

>

>

Wyjście:

Type before conversion : Type after conversion :>

5. Zastosowanie metody __str__().

Składnia: I liczba całkowita.__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))>

>

>

Wyjście:

Type before conversion : Type after conversion :>

6. Korzystanie ze str.isdigit()

Python3


otwórz plik z Javą



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

>

>

Wyjście

1234>

7. Używanie metody Join():

Metoda Join() służy do konwertowania listy liczb całkowitych na ciąg znaków. Liczbę całkowitą konwertujemy na listę znaków za pomocą funkcji list(), a następnie łączymy je za 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))>

>

>

Wyjście

Type before conversion : Type after conversion :>

Złożoność czasowa: O(N) gdzie n jest liczbą cyfr liczby całkowitej.

Złożoność przestrzenna: O(N) Jak musimy utworzyć listę znaków zawierającą n elementów,