logo

Python の f 文字列

Python は、と呼ばれる強力な機能を提供します。 F 文字列 (フォーマットされた文字列リテラル) を使用して、文字列のフォーマットと補間を簡素化します。 F 文字列 Python 3.6 で導入され、式や変数を文字列に直接埋め込むための簡潔かつ直感的な方法を提供します。 f-strings の背後にある考え方は、文字列補間をより簡単にすることです。

Python で f-string を使用する方法

f-string を作成するには、文字列の前に文字を付けます。 f 。文字列自体は、次の場合とほぼ同じ方法でフォーマットできます。 str.format() 。 F 文字列は、書式設定のために文字列リテラル内に Python 式を埋め込むための簡潔で便利な方法を提供します。

Python で f-string を使用して変数を出力する

以下の例では、print() メソッド内で f-string を使用して文字列を出力しています。 f-strings 内で変数値を使用するには中括弧を使用します。そのため、以下のコードに示すように、変数「val」を「Geeks」で定義し、これを内部で使用します。 「ヴァル」 「オタク」 。同様に、 '名前' そして 2 番目の print ステートメント内の変数。



パイソン
# Python3 program introducing f-string val = 'Geeks' print(f'{val}for{val} is a portal for {val}.') name = 'Tushar' age = 23 print(f'Hello, My name is {name} and I'm {age} years old.')>

出力

techcodeview.com is a portal for Geeks. Hello, My name is Tushar and I'm 23 years old.>

Pythonでf-stringを使用して日付を出力する

この例では、今日の日付を次のコマンドを使用して出力しました。 日付時刻 モジュール Pythonで f文字列。 そのために、まず datetime モジュールをインポートし、その後 f-sting を使用して日付を出力します。 F ストリングの内側 '今日' 現在の日付が割り当てられ、 %B %d 、 そして %そして を表します 満月 月の日 、 そして それぞれ。

パイソン
# Prints today's date with help # of datetime library import datetime today = datetime.datetime.today() print(f'{today:%B %d, %Y}')>

出力

March 06, 2024>

注記: F 文字列は、最も一般的に使用される 2 つの文字列書式設定メカニズムである % 書式設定と str.format() よりも高速です。

Python の f 文字列内の引用符

Python の f-string であらゆる種類の引用符を使用するには、式内で使用される引用符が f-string で使用される引用符と同じでないことを確認する必要があります。

パイソン
print(f''techcodeview.com'') print(f'''Geeks'for'Geeks''') print(f'''Geeks'for'Geeks''')>

出力

'techcodeview.com' Geeks'for'Geeks Geeks'for'Geeks>

Python で f-String を使用して式を評価する

Python では f 文字列を使用して式を評価することもできます。そのためには、f-string の中括弧内に式を記述する必要があります。評価された結果は、以下のコードの出力に示すように出力されます。

パイソン
english = 78 maths = 56 hindi = 85 print(f'Ram got total marks {english + maths + hindi} out of 300')>

出力

gimpの選択を解除する方法
Ram got total marks 219 out of 300>

Python で f-string を使用する際のエラー

Python の f 文字列のバックスラッシュ

Python の f-string、バックスラッシュをフォーマット文字列に直接使用することはできません。

パイソン
f'newline: {ord('
')'>

出力

Traceback (most recent call last):  Python Shell, prompt 29, line 1 Syntax Error: f-string expression part cannot include a backslash: , line 1, pos 0>

ただし、回避策として変数にバックスラッシュを入れることもできます。

パイソン
newline = ord('
') print(f'newline: {newline}')>

出力

newline: 10>

Pythonのf-stringのインラインコメント

F 文字列式内でコメントを使用することはできません。エラーが発生します:

パイソン
f'techcodeview.com is {5*2 + 3 #geeks-5} characters.'>

出力:

Hangup (SIGHUP)  File 'Solution.py', line 1  f'techcodeview.com is {5*2 + 3 #geeks-5} characters.'  ^ SyntaxError: f-string expression part cannot include '#'>

Python で f-string を使用して中括弧を出力する

f 文字列の出力に中括弧を表示したい場合は、f 文字列で二重中括弧を使用する必要があります。以下のコードに示すように、中括弧の単一のペアごとに二重中括弧を入力する必要があることに注意してください。

パイソン
# Printing single braces print(f'{{Hello, Geek}}') # Printing double braces print(f'{{{{Hello, Geek}}}}')>

出力

{Hello, Geek} {{Hello, Geek}}>

Python で f-string を使用して辞書のキーと値を印刷する

辞書を操作するとき、f 文字列で二重引用符 () を使用している場合は、Python の f 文字列内のキーには一重引用符 (‘) を使用する必要があり、その逆も同様であることを確認する必要があります。そうしないと、構文エラーがスローされます。

ビープラスツリー
パイソン
Geek = { 'Id': 112, 'Name': 'Harsh'} print(f'Id of {Geek['Name']} is {Geek['Id']}')>

出力

Hangup (SIGHUP)  File 'Solution.py', line 4  print(f'Id of {Geek['Name']} is {Geek['Id']}')  ^ SyntaxError: invalid syntax>

f-string と key に同じ種類の引用符を使用する

パイソン
Geek = { 'Id': 112, 'Name': 'Harsh'} print(f'Id of {Geek['Name']} is {Geek['Id']}')>

出力

Id of Harsh is 112>