


Comment éviter « TypeError : tous les arguments ne sont pas convertis » lors de l'utilisation du formatage d'espace réservé en Python
Oct 18, 2024 pm 01:04 PMTypeError When Substituting Placeholder with % Formatting
When attempting to substitute a placeholder like {0} using % formatting, developers may encounter the following error: "TypeError: not all arguments converted during string formatting." This error stems from improper formatting, specifically due to a mix-up between old-style % formatting and new-style {} formatting.
Old-style % formatting employs placeholders like %d for formatting, as exemplified below:
'It will cost $%d dollars.' % 95
However, when utilizing multiple values, they must be provided as a tuple:
"'%s' is longer than '%s'" % (name1, name2)
On the other hand, new-style {} formatting employs placeholders like {} and the .format method. It's crucial to avoid mixing these two styles. If the template string contains {} placeholders, .format should be used, not %.
# Correct: 'It will cost ${0} dollars.'.format(95) "'{0}' is longer than '{1}'".format(name1, name2) # Incorrect (Do not mix % and {}): 'It will cost ${0} dollars.' % 95 "'%0' is longer than '%1'" % (name1, name2)
By adhering to these formatting guidelines, developers can resolve the "TypeError: not all arguments converted during string formatting" error and format their strings correctly.
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!

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Comment utiliser la belle soupe pour analyser HTML?

Comment télécharger des fichiers dans Python

Comment utiliser Python pour trouver la distribution ZIPF d'un fichier texte

Comment travailler avec des documents PDF à l'aide de Python

Comment se cacher en utilisant Redis dans les applications Django

Comment effectuer l'apprentissage en profondeur avec TensorFlow ou Pytorch?

Présentation de la boîte à outils en langage naturel (NLTK)
