Skip to content

Instantly share code, notes, and snippets.

@mccurcio
Created December 22, 2021 17:18
Show Gist options
  • Save mccurcio/cae051132c16beb18166d43abbd57ebd to your computer and use it in GitHub Desktop.
Save mccurcio/cae051132c16beb18166d43abbd57ebd to your computer and use it in GitHub Desktop.
f-Strings Since Python 3.6
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Use the variables directly inside the braces.\n\nname = \"Eric\"\nage = 25\n\na = f\"Hello, {name}. You are {age}.\"\nprint(a, '\\n')\n\npi = 3.14159\nb = f\"Pi is {pi:.3f}\"\nprint(b, '\\n')\n\n# f-Strings are evaluated at runtime, which allows expressions\nc = f\"The value is {2*60}\"\nprint(c)",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "Hello, Eric. You are 25. \n\nPi is 3.142 \n\nThe value is 120\n",
"name": "stdout"
}
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3 (ipykernel)",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.12",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "f-Strings Since Python 3.6",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment