「TypeError: String Formatting Mismatch」エラーを解決するにはどうすればよいですか?

Patricia Arquette
リリース: 2024-10-18 12:08:26
オリジナル
509 人が閲覧しました

How to Resolve the \

Understanding the TypeError: String Formatting Mismatch

When trying to incorporate placeholders like {0} in string formatting, it's essential to understand the nuances of the formatting method being used. The error message "TypeError: not all arguments converted during string formatting" typically arises when the %-based formatting method is misused.

Old-style % Formatting

The % formatting method employs % codes to specify formatting operations. When formatting a single value, it can be included directly in the string:

<code class="python">'It will cost $%d dollars.' % 95</code>
ログイン後にコピー

However, for multiple values, a tuple must be provided:

<code class="python">&quot;'%s' is longer than '%s'&quot; % (name1, name2)</code>
ログイン後にコピー

New-style {} Formatting

In contrast, the {} formatting method utilizes {} placeholders and the .format() method. It's crucial to avoid mixing these methods. If the "template" string contains {} placeholders, use .format(), not %.

<code class="python"># Values as method arguments
'It will cost ${0} dollars.'.format(95)
&quot;'{0}' is longer than '{1}'&quot;.format(name1, name2)</code>
ログイン後にコピー

以上が「TypeError: String Formatting Mismatch」エラーを解決するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!