Skip to content

Commit 45d5b4f

Browse files
remilapeyrearturoescaip
authored andcommitted
Use f-strings in argparse HOWTO (pythonGH-20070)
1 parent d8f6089 commit 45d5b4f

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

Doc/howto/argparse.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ Our program keeps growing in complexity::
353353
args = parser.parse_args()
354354
answer = args.square**2
355355
if args.verbose:
356-
print("the square of {} equals {}".format(args.square, answer))
356+
print(f"the square of {args.square} equals {answer}")
357357
else:
358358
print(answer)
359359

@@ -387,9 +387,9 @@ multiple verbosity values, and actually get to use them::
387387
args = parser.parse_args()
388388
answer = args.square**2
389389
if args.verbosity == 2:
390-
print("the square of {} equals {}".format(args.square, answer))
390+
print(f"the square of {args.square} equals {answer}")
391391
elif args.verbosity == 1:
392-
print("{}^2 == {}".format(args.square, answer))
392+
print(f"{args.square}^2 == {answer}")
393393
else:
394394
print(answer)
395395

@@ -421,9 +421,9 @@ Let's fix it by restricting the values the ``--verbosity`` option can accept::
421421
args = parser.parse_args()
422422
answer = args.square**2
423423
if args.verbosity == 2:
424-
print("the square of {} equals {}".format(args.square, answer))
424+
print(f"the square of {args.square} equals {answer}")
425425
elif args.verbosity == 1:
426-
print("{}^2 == {}".format(args.square, answer))
426+
print(f"{args.square}^2 == {answer}")
427427
else:
428428
print(answer)
429429

@@ -461,9 +461,9 @@ verbosity argument (check the output of ``python --help``)::
461461
args = parser.parse_args()
462462
answer = args.square**2
463463
if args.verbosity == 2:
464-
print("the square of {} equals {}".format(args.square, answer))
464+
print(f"the square of {args.square} equals {answer}")
465465
elif args.verbosity == 1:
466-
print("{}^2 == {}".format(args.square, answer))
466+
print(f"{args.square}^2 == {answer}")
467467
else:
468468
print(answer)
469469

@@ -529,9 +529,9 @@ Let's fix::
529529

530530
# bugfix: replace == with >=
531531
if args.verbosity >= 2:
532-
print("the square of {} equals {}".format(args.square, answer))
532+
print(f"the square of {args.square} equals {answer}")
533533
elif args.verbosity >= 1:
534-
print("{}^2 == {}".format(args.square, answer))
534+
print(f"{args.square}^2 == {answer}")
535535
else:
536536
print(answer)
537537

@@ -566,9 +566,9 @@ Let's fix that bug::
566566
args = parser.parse_args()
567567
answer = args.square**2
568568
if args.verbosity >= 2:
569-
print("the square of {} equals {}".format(args.square, answer))
569+
print(f"the square of {args.square} equals {answer}")
570570
elif args.verbosity >= 1:
571-
print("{}^2 == {}".format(args.square, answer))
571+
print(f"{args.square}^2 == {answer}")
572572
else:
573573
print(answer)
574574

@@ -606,9 +606,9 @@ not just squares::
606606
args = parser.parse_args()
607607
answer = args.x**args.y
608608
if args.verbosity >= 2:
609-
print("{} to the power {} equals {}".format(args.x, args.y, answer))
609+
print(f"{args.x} to the power {args.y} equals {answer}")
610610
elif args.verbosity >= 1:
611-
print("{}^{} == {}".format(args.x, args.y, answer))
611+
print(f"{args.x}^{args.y} == {answer}")
612612
else:
613613
print(answer)
614614

@@ -645,9 +645,9 @@ to display *more* text instead::
645645
args = parser.parse_args()
646646
answer = args.x**args.y
647647
if args.verbosity >= 2:
648-
print("Running '{}'".format(__file__))
648+
print(f"Running '{__file__}'")
649649
if args.verbosity >= 1:
650-
print("{}^{} == ".format(args.x, args.y), end="")
650+
print(f"{args.x}^{args.y} == ", end="")
651651
print(answer)
652652

653653
Output:
@@ -688,9 +688,9 @@ which will be the opposite of the ``--verbose`` one::
688688
if args.quiet:
689689
print(answer)
690690
elif args.verbose:
691-
print("{} to the power {} equals {}".format(args.x, args.y, answer))
691+
print(f"{args.x} to the power {args.y} equals {answer}")
692692
else:
693-
print("{}^{} == {}".format(args.x, args.y, answer))
693+
print(f"{args.x}^{args.y} == {answer}")
694694

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

0 commit comments

Comments
 (0)