logo

Konvertieren Sie eine Ganzzahl in einen String in Python

In Python kann eine Ganzzahl mithilfe der integrierten Funktion in einen String umgewandelt werden str() Funktion. Die Funktion str() nimmt alle auf str() ist nicht der einzige Weg, dies zu tun. Diese Art der Konvertierung kann auch mit durchgeführt werden %S Stichwort, das .Format Funktion oder Verwendung F-Saite Funktion.

Nachfolgend finden Sie eine Liste möglicher Möglichkeiten zum Konvertieren einer Ganzzahl in eine Zeichenfolge in Python:

1. Verwendung der Funktion str()



Syntax: str(integer_value)

Beispiel:

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

>

>

Ausgabe:

Type of variable before conversion : Type After conversion :>

2. Verwendung des Schlüsselworts %s

Syntax: %s % Ganzzahl

Beispiel:

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

>

>

Ausgabe:

Type of variable before conversion : Type after conversion :>

3. Verwenden der Funktion .format()

Syntax: ‘{}’.format(integer)

Beispiel:

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

>

>

Ausgabe:

Type before conversion : Type after conversion :>

4. F-String verwenden

Syntax: f'{integer}’

Beispiel:

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

>

>

Ausgabe:

Type before conversion : Type after conversion :>

5. Verwendung der Methode __str__()

Syntax: I 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))>

>

>

Ausgabe:

Type before conversion : Type after conversion :>

6. Verwendung von str.isdigit()

Python3


Pseudocode Java



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

>

>

Ausgabe

1234>

7.Verwenden der Methode „join()“:

Die Methode join() wird verwendet, um eine Liste von Ganzzahlen in eine Zeichenfolge umzuwandeln. Wir wandeln die Ganzzahl mit der Funktion list() in eine Liste von Zeichen um und verbinden sie dann mit der Methode 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))>

>

>

Ausgabe

Type before conversion : Type after conversion :>

Zeitkomplexität: O(N) Dabei ist n die Anzahl der Ziffern der Ganzzahl.

Raumkomplexität:O(N) als Wir müssen eine Liste von Zeichen erstellen, die n Elemente enthält.