8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

f-stringsでdict使う場合のエラー解消

Posted at

f-stringsでdictを使おうとした時にハマったエラー

何気なくいつも通りプログラム中でf-stringsを使おうとしたところ以下のようなエラーが発生

実行スクリプト例
ex_dict = {'a':'b'}

print(f'{ex_dict['a']}')
エラー文
SyntaxError: f-string: unmatched '['

原因と解決法

dictの要素指定のシングルクオーテーションをf-stringsのシングルクォーテーション内で使ってしまったことによるSyntaxエラー。
外側 or 中をダブルクォーテーション""にすることで解決。

ただのstringであればエスケープシーケンスを使うこともできるがdictの要素指定の場合はエスケープシーケンスではエラーを吐かれてしまうので注意

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?