diff options
author | RĂ©mi Lapeyre <remi.lapeyre@lenstra.fr> | 2020-05-21 04:22:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-21 04:22:59 (GMT) |
commit | 7efb826c3e54edf10315e4baf5e96fe9a3729da4 (patch) | |
tree | 2e60928c3f8127a50364adab0ce4d0ca5dc6ded3 /Doc/howto | |
parent | df2e0ff0d63b07a1c8fdfd3674d99bd4f2fb69c5 (diff) | |
download | cpython-7efb826c3e54edf10315e4baf5e96fe9a3729da4.zip cpython-7efb826c3e54edf10315e4baf5e96fe9a3729da4.tar.gz cpython-7efb826c3e54edf10315e4baf5e96fe9a3729da4.tar.bz2 |
Use f-strings in argparse HOWTO (GH-20070)
Diffstat (limited to 'Doc/howto')
-rw-r--r-- | Doc/howto/argparse.rst | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/Doc/howto/argparse.rst b/Doc/howto/argparse.rst index e78a022..76d8e6b 100644 --- a/Doc/howto/argparse.rst +++ b/Doc/howto/argparse.rst @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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: @@ -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: |