Comment convertir un nombre en chaîne en Python ?

WBOY
Libérer: 2023-08-19 20:45:25
avant
6081 Les gens l'ont consulté

Comment convertir un nombre en chaîne en Python ?

Pour convertir un nombre en chaîne, il existe plusieurs façons. Regardons-les un par un.

Convertir le nombre en chaîne en utilisant format()

La traduction chinoise de

Exemple

est :

Exemple

Dans cet exemple, nous utiliserons la méthode format() pour convertir un nombre en chaîne -

# Integer to be converted
n = 60

# Display the integer and it's type
print("Integer = ",n)
print("Type= ", type(n))

# Convert the integer to string and display the type
myStr = "{}".format(n)
print("\nString = ", myStr)
print("Type = ", type(myStr))
Copier après la connexion

Sortie

Integer =  60
Type=  <class 'int'>
String =  60
Type =  <class 'str'>
Copier après la connexion

Convertir un nombre en chaîne à l'aide de la fonction str()

La traduction chinoise de

Exemple

est :

Exemple

Dans cet exemple, nous utiliserons la méthode str() pour convertir un nombre en chaîne −

# Integer to be converted
n = 25

# Display the integer and it's type
print("Integer = ",n)
print("Type= ", type(n))

# Convert the integer to string using str() and display the type
myStr = str(n)
print("\nString = ", myStr)
print("Type = ", type(myStr))
Copier après la connexion

Sortie

Integer =  25
Type=  <class 'int'>

String =  25
Type =  <class 'str'>
Copier après la connexion

Convertir le nombre en chaîne en utilisant le format %s

La traduction chinoise de

Exemple

est :

Exemple

Dans cet exemple, nous utiliserons le spécificateur de format %s pour convertir un nombre en chaîne −

# Integer to be converted
n = 90

# Display the integer and it's type
print("Integer = ",n)
print("Type= ", type(n))

# Convert the integer to string using %s and display the type
myStr = "% s" % n
print("\nString = ", myStr)
print("Type = ", type(myStr))
Copier après la connexion

Sortie

Integer =  90
Type=  <class 'int'>

String =  90
Type =  <class 'str'>
Copier après la connexion

Convertir un nombre en chaîne en utilisant __str__()

La traduction chinoise de

Exemple

est :

Exemple

Dans cet exemple, nous allons convertir un nombre en chaîne en utilisant la méthode __string__() en Python -

# Integer to be converted
n = 150

# Display the integer and it's type
print("Integer = ",n)
print("Type= ", type(n))

# Convert the integer to string using __str__() and display the type
myStr = n.__str__()
print("\nString = ", myStr)
print("Type = ", type(myStr))
Copier après la connexion

Sortie

Integer =  150
Type=  <class 'int'>

String =  150
Type =  <class 'str'>
Copier après la connexion

Convertir le nombre en chaîne à l'aide de f-string

La traduction chinoise de

Exemple

est :

Exemple

Dans cet exemple, nous utiliserons f-string pour convertir un nombre en chaîne −

# Integer to be converted
n = 21

# Display the integer and it's type
print("Integer = ",n)
print("Type= ", type(n))

# Convert the integer to string using f-string and display the type
myStr = f'{n}'
print("\nString = ", myStr)
print("Type = ", type(myStr))
Copier après la connexion

Sortie

Integer =  21
Type=  <class 'int'>

String =  21
Type =  <class 'str'>
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:tutorialspoint.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!