[Python-checkins] (no subject)
Rémi Lapeyre
webhook-mailer at python.org
Thu May 21 00:23:04 EDT 2020
To: python-checkins at python.org
Subject: Use f-strings in argparse HOWTO (GH-20070)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
https://github.com/python/cpython/commit/7efb826c3e54edf10315e4baf5e96fe9a372=
9da4
commit: 7efb826c3e54edf10315e4baf5e96fe9a3729da4
branch: master
author: R=C3=A9mi Lapeyre <remi.lapeyre at lenstra.fr>
committer: GitHub <noreply at github.com>
date: 2020-05-20T21:22:59-07:00
summary:
Use f-strings in argparse HOWTO (GH-20070)
files:
M Doc/howto/argparse.rst
diff --git a/Doc/howto/argparse.rst b/Doc/howto/argparse.rst
index e78a022b372fa..76d8e6be42935 100644
--- a/Doc/howto/argparse.rst
+++ b/Doc/howto/argparse.rst
@@ -353,7 +353,7 @@ Our program keeps growing in complexity::
args =3D parser.parse_args()
answer =3D args.square**2
if args.verbose:
- print("the square of {} equals {}".format(args.square, answer))
+ print(f"the square of {args.square} equals {answer}")
else:
print(answer)
=20
@@ -387,9 +387,9 @@ multiple verbosity values, and actually get to use them::
args =3D parser.parse_args()
answer =3D args.square**2
if args.verbosity =3D=3D 2:
- print("the square of {} equals {}".format(args.square, answer))
+ print(f"the square of {args.square} equals {answer}")
elif args.verbosity =3D=3D 1:
- print("{}^2 =3D=3D {}".format(args.square, answer))
+ print(f"{args.square}^2 =3D=3D {answer}")
else:
print(answer)
=20
@@ -421,9 +421,9 @@ Let's fix it by restricting the values the ``--verbosity`=
` option can accept::
args =3D parser.parse_args()
answer =3D args.square**2
if args.verbosity =3D=3D 2:
- print("the square of {} equals {}".format(args.square, answer))
+ print(f"the square of {args.square} equals {answer}")
elif args.verbosity =3D=3D 1:
- print("{}^2 =3D=3D {}".format(args.square, answer))
+ print(f"{args.square}^2 =3D=3D {answer}")
else:
print(answer)
=20
@@ -461,9 +461,9 @@ verbosity argument (check the output of ``python --help``=
)::
args =3D parser.parse_args()
answer =3D args.square**2
if args.verbosity =3D=3D 2:
- print("the square of {} equals {}".format(args.square, answer))
+ print(f"the square of {args.square} equals {answer}")
elif args.verbosity =3D=3D 1:
- print("{}^2 =3D=3D {}".format(args.square, answer))
+ print(f"{args.square}^2 =3D=3D {answer}")
else:
print(answer)
=20
@@ -529,9 +529,9 @@ Let's fix::
=20
# bugfix: replace =3D=3D with >=3D
if args.verbosity >=3D 2:
- print("the square of {} equals {}".format(args.square, answer))
+ print(f"the square of {args.square} equals {answer}")
elif args.verbosity >=3D 1:
- print("{}^2 =3D=3D {}".format(args.square, answer))
+ print(f"{args.square}^2 =3D=3D {answer}")
else:
print(answer)
=20
@@ -566,9 +566,9 @@ Let's fix that bug::
args =3D parser.parse_args()
answer =3D args.square**2
if args.verbosity >=3D 2:
- print("the square of {} equals {}".format(args.square, answer))
+ print(f"the square of {args.square} equals {answer}")
elif args.verbosity >=3D 1:
- print("{}^2 =3D=3D {}".format(args.square, answer))
+ print(f"{args.square}^2 =3D=3D {answer}")
else:
print(answer)
=20
@@ -606,9 +606,9 @@ not just squares::
args =3D parser.parse_args()
answer =3D args.x**args.y
if args.verbosity >=3D 2:
- print("{} to the power {} equals {}".format(args.x, args.y, answer))
+ print(f"{args.x} to the power {args.y} equals {answer}")
elif args.verbosity >=3D 1:
- print("{}^{} =3D=3D {}".format(args.x, args.y, answer))
+ print(f"{args.x}^{args.y} =3D=3D {answer}")
else:
print(answer)
=20
@@ -645,9 +645,9 @@ to display *more* text instead::
args =3D parser.parse_args()
answer =3D args.x**args.y
if args.verbosity >=3D 2:
- print("Running '{}'".format(__file__))
+ print(f"Running '{__file__}'")
if args.verbosity >=3D 1:
- print("{}^{} =3D=3D ".format(args.x, args.y), end=3D"")
+ print(f"{args.x}^{args.y} =3D=3D ", end=3D"")
print(answer)
=20
Output:
@@ -688,9 +688,9 @@ which will be the opposite of the ``--verbose`` one::
if args.quiet:
print(answer)
elif args.verbose:
- print("{} to the power {} equals {}".format(args.x, args.y, answer))
+ print(f"{args.x} to the power {args.y} equals {answer}")
else:
- print("{}^{} =3D=3D {}".format(args.x, args.y, answer))
+ print(f"{args.x}^{args.y} =3D=3D {answer}")
=20
Our program is now simpler, and we've lost some functionality for the sake of
demonstration. Anyways, here's the output:
More information about the Python-checkins
mailing list