% 記号は、Python でさまざまなデータ型と構成で使用されます。 %s は、具体的には文字列の連結を実行するために使用されます。これにより、文字列内の値をフォーマットすることができます。文字列内に別の文字列を組み込むために使用されます。値から文字列への型変換が自動的に行われます。
%s 演算子は、文字列を指定する場所に置かれます。文字列に追加する値の数は、文字列値の末尾の % 演算子の後の括弧内に指定された数と同じである必要があります。
次の Python コードは、文字列の書式設定を実行する方法を示しています。
%s の単純な使用法
Python3
# declaring a string variable> name>=> 'Geek'> # append a string within a string> print>(>'Hey, %s!'> %> name)> |
>
JavaScriptのif文
>
出力
Hey, Geek!>
複数の%s
%s 演算子を使用して、単一の文字列内に複数の文字列を追加することもできます。文字列は、%s 記号があるかっこ内の位置の順序で置き換えられます。これは、次のコード スニペットを使用して説明されています。
Python3
# declaring a string variable> var1>=> 'Geek!'> var2>=> 'Geeks for Geeks'> # append multiple strings within a string> print>(>'Hello %s Are you enjoying being at %s for preparations.'> %> (var1, var2))> |
>
>
出力
こんにちはオタク! Geeks for Geeks での準備を楽しんでいますか。
文字列を %s にマッピングしています
ただし、この演算子の出現数は、% 記号の後に置換される文字列の数と同じである必要があります。それ以外の場合は、「TypeError: フォーマット文字列の引数が足りません」というタイプのエラーがスローされます。
Python3
ハッシュセットJavaとは何ですか
# declaring string variables> str1>=> 'Understanding'> str2>=> '%s'> str3>=> 'at'> str4>=> 'techcodeview.com'> # concatenating strings but %s not equal to string variables> final_str>=> '%s %s %s %s'> %> (str1, str3, str4)> # printing the final string> print>(>'Concatenating multiple strings using Python '%s' operator:
'>)> print>(final_str)> |
>
>
エラー
トレースバック (最後の呼び出し):
ファイル /home/c7b65fabd2ad00163eba70bbc39685d3.py、8 行目
Final_str = %s %s %s %s % (str1、str3、str4)
TypeError: フォーマット文字列の引数が不足しています
正しいコード
Python3
ギガバイトとメガバイトの違い
# declaring string variables> str1>=> 'Understanding'> str2>=> '%s'> str3>=> 'at'> str4>=> 'techcodeview.com'> # concatenating strings> final_str>=> '%s %s %s %s'> %> (str1, str2, str3, str4)> # printing the final string> print>(>'Concatenating multiple strings using Python '%s' operator:
'>)> print>(final_str)> |
>
>
出力
Concatenating multiple strings using Python '%s' operator: Understanding %s at techcodeview.com>
辞書を使用して %s を注文します
文字列は、出力で辞書キーを使用して追加された順序で出力されます。
Python3
# declaring string variables with dictionary> dct>=> {>'str1'>:>'at'>,> >'str2'>:>'techcodeview.com'>,> >'str3'>:>'Understanding'>,> >'str4'>:>'%s'>}> # concatenating strings> final_str>=> '%(str3)s %(str4)s %(str1)s %(str2)s'> %> dct> # printing the final string> print>(>'Concatenating multiple strings using Python '%s' operator:
'>)> print>(final_str)> |
>
>
出力
Concatenating multiple strings using Python '%s' operator: Understanding %s at techcodeview.com>
としてリストする %s の文字列
非文字列演算子は、Python の %s 記号を使用して書式設定することもできます。この演算子を使用して、タプルの挿入とフォーマットの両方を行うこともできます。
Python3
# declaring string variables> str1>=> 'Understanding'> str2>=> 'integers'> str3>=> 'at'> str4>=> 'techcodeview.com = '> # declaring list variables> lst>=> [>1>,>2>,>3>]> # concatenating strings as well as list> final_str>=> '%s %s %s %s %s'> %> (str1, str2, str3, str4, lst)> # printing the final string> print>(>'Concatenating multiple values using Python '%s' operator:
'>)> print>(final_str)> |
Javaで配列に追加する
>
>
出力
Concatenating multiple values using Python '%s' operator: Understanding integers at techcodeview.com = [1, 2, 3]>