Skip to content

Use f-strings in argparse HOWTO #20070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions Doc/howto/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ Our program keeps growing in complexity::
args = parser.parse_args()
answer = 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)

Expand Down Expand Up @@ -387,9 +387,9 @@ multiple verbosity values, and actually get to use them::
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
print("the square of {} equals {}".format(args.square, answer))
print(f"the square of {args.square} equals {answer}")
elif args.verbosity == 1:
print("{}^2 == {}".format(args.square, answer))
print(f"{args.square}^2 == {answer}")
else:
print(answer)

Expand Down Expand Up @@ -421,9 +421,9 @@ Let's fix it by restricting the values the ``--verbosity`` option can accept::
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
print("the square of {} equals {}".format(args.square, answer))
print(f"the square of {args.square} equals {answer}")
elif args.verbosity == 1:
print("{}^2 == {}".format(args.square, answer))
print(f"{args.square}^2 == {answer}")
else:
print(answer)

Expand Down Expand Up @@ -461,9 +461,9 @@ verbosity argument (check the output of ``python --help``)::
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
print("the square of {} equals {}".format(args.square, answer))
print(f"the square of {args.square} equals {answer}")
elif args.verbosity == 1:
print("{}^2 == {}".format(args.square, answer))
print(f"{args.square}^2 == {answer}")
else:
print(answer)

Expand Down Expand Up @@ -529,9 +529,9 @@ Let's fix::

# bugfix: replace == with >=
if args.verbosity >= 2:
print("the square of {} equals {}".format(args.square, answer))
print(f"the square of {args.square} equals {answer}")
elif args.verbosity >= 1:
print("{}^2 == {}".format(args.square, answer))
print(f"{args.square}^2 == {answer}")
else:
print(answer)

Expand Down Expand Up @@ -566,9 +566,9 @@ Let's fix that bug::
args = parser.parse_args()
answer = args.square**2
if args.verbosity >= 2:
print("the square of {} equals {}".format(args.square, answer))
print(f"the square of {args.square} equals {answer}")
elif args.verbosity >= 1:
print("{}^2 == {}".format(args.square, answer))
print(f"{args.square}^2 == {answer}")
else:
print(answer)

Expand Down Expand Up @@ -606,9 +606,9 @@ not just squares::
args = parser.parse_args()
answer = args.x**args.y
if args.verbosity >= 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 >= 1:
print("{}^{} == {}".format(args.x, args.y, answer))
print(f"{args.x}^{args.y} == {answer}")
else:
print(answer)

Expand Down Expand Up @@ -645,9 +645,9 @@ to display *more* text instead::
args = parser.parse_args()
answer = args.x**args.y
if args.verbosity >= 2:
print("Running '{}'".format(__file__))
print(f"Running '{__file__}'")
if args.verbosity >= 1:
print("{}^{} == ".format(args.x, args.y), end="")
print(f"{args.x}^{args.y} == ", end="")
print(answer)

Output:
Expand Down Expand Up @@ -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("{}^{} == {}".format(args.x, args.y, answer))
print(f"{args.x}^{args.y} == {answer}")

Our program is now simpler, and we've lost some functionality for the sake of
demonstration. Anyways, here's the output:
Expand Down