Convertiți un întreg în șir în Python

În Python, un număr întreg poate fi convertit într-un șir folosind funcția încorporată str() funcţie. Funcția str() preia orice str() nu este singura modalitate de a face acest lucru. Acest tip de conversie se poate face și folosind %s cuvânt cheie, .format funcția sau utilizarea f-string funcţie.

Mai jos este lista modalităților posibile de a converti un număr întreg în șir în python:

1. Folosind funcția str().

Sintaxă: str(valoare_întregă)

Exemplu:

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

Ieșire:

Type of variable before conversion : Type After conversion : 

2. Folosind cuvântul cheie %s

Sintaxă: %s % întreg

Exemplu:

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

Ieșire:

Type of variable before conversion : Type after conversion : 

3. Folosind funcția .format().

Sintaxă: „{}”.format(întreg)

Exemplu:

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

Ieșire:

Type before conversion : Type after conversion : 

4. Folosind f-string

Sintaxă: f'{integer}’

Exemplu:

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

Ieșire:

Type before conversion : Type after conversion : 

5. Folosind metoda __str__().

Sintaxă: I întreg.__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))>

Ieșire:

Type before conversion : Type after conversion : 

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

Ieșire

1234 

7. Folosind metoda join():

Metoda join() este folosită pentru a converti o listă de numere întregi într-un șir. Convertim întregul într-o listă de caractere folosind funcția list() și apoi le unim folosind metoda 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))>

Ieșire

Type before conversion : Type after conversion : 

Complexitatea timpului: O(N) unde n este numărul de cifre din întreg.

Complexitatea spațiului: O(N) la fel de trebuie să creăm o listă de caractere care să aibă n elemente,