|
416 | 416 | }, |
417 | 417 | "outputs": [], |
418 | 418 | "source": [ |
419 | | - "print(\"Refractive index estimate: {0:.5f} ± {1:.5f}\".format(nOpt, nAlpha))" |
| 419 | + "print(f\"Refractive index estimate: {nOpt:.5f} ± {nAlpha:.5f}\")" |
420 | 420 | ] |
421 | 421 | }, |
422 | 422 | { |
|
427 | 427 | } |
428 | 428 | }, |
429 | 429 | "source": [ |
430 | | - "The expressions in curly brackets `{}` are known as a *format specifiers*, which format the arguments to the `format` method appended to the end of the string. The `.format()` at the end tells the interpreter that formatting information is contained in the string; in each format specifier, the number the precedes the colon `:` is the position index of the argument to be formatted. The expression that follows the colon then tells the interpreter how to format the variable. For example, the specifier `.5f` will represent the argument in floating-point notation to five decimal places. Examples of the most common numerical format strings are below." |
| 430 | + "The `f` that precedes the expression in quotation marks indicates that this is a formatted string literal, or [f-string](https://docs.python.org/3/reference/lexical_analysis.html#f-strings). F-strings have the format `f\"Text {variable:format}\"`, where the curly brackets `{}` include instructions for printing the value of a variable with a specified format. For example, the specifier `.5f` will represent the argument in floating-point notation to five decimal places. Examples of the most common numerical format strings are below." |
431 | 431 | ] |
432 | 432 | }, |
433 | 433 | { |
|
440 | 440 | }, |
441 | 441 | "outputs": [], |
442 | 442 | "source": [ |
443 | | - "print(\"Field width=10, precision=5, floating point format: {0:.5f} ± {1:.5f}\".format(nOpt, nAlpha))\n", |
444 | | - "print(\"Field width=10, precision=5, scientific notation format: {0:.5e} ± {1:.5e}\".format(nOpt, nAlpha))\n", |
445 | | - "print(\"Field width=10, precision=5, the more compact of e or f format: {0:.5g} ± {1:.5g}\".format(nOpt, nAlpha))\n", |
446 | | - "print(\"Field width=10, signed integer format: {0:d} ± {1:d}\".format(nOpt.astype(int), nAlpha.astype(int)))" |
| 443 | + "print(\"Field width=10, precision=5, floating point format: \"\n", |
| 444 | + " f\"{nOpt:.5f} ± {nAlpha:.5f}\")\n", |
| 445 | + "print(\"Field width=10, precision=5, scientific notation format: \"\n", |
| 446 | + " f\"{nOpt:.5e} ± {nAlpha:.5e}\")\n", |
| 447 | + "print(\"Field width=10, precision=5, the more compact of e or f format: \"\n", |
| 448 | + " f\"{nOpt:.5g} ± {nAlpha:.5g}\")\n", |
| 449 | + "print(\"Field width=10, signed integer format: \"\n", |
| 450 | + " f\"{nOpt.astype(int):d} ± {nAlpha.astype(int):d}\")" |
447 | 451 | ] |
448 | 452 | }, |
449 | 453 | { |
|
505 | 509 | "res = delta_deg - model(theta_deg,nOpt,alpha_deg_const)\n", |
506 | 510 | "normres = res/err_deg\n", |
507 | 511 | "chisq = np.sum(normres**2)\n", |
508 | | - "print(\"chi-squared = {0:.1f}\".format(chisq))" |
| 512 | + "print(f\"chi-squared = {chisq:.1f}\")" |
509 | 513 | ] |
510 | 514 | }, |
511 | 515 | { |
|
533 | 537 | "Ndata = np.size(delta_deg)\n", |
534 | 538 | "Npar = np.size(nOpt)\n", |
535 | 539 | "dof = Ndata - Npar\n", |
536 | | - "print(\"dof = {0:d}\".format(dof))" |
| 540 | + "print(f\"dof = {dof:d}\")" |
537 | 541 | ] |
538 | 542 | }, |
539 | 543 | { |
|
558 | 562 | "outputs": [], |
559 | 563 | "source": [ |
560 | 564 | "cdf = chi2.cdf(chisq,dof)\n", |
561 | | - "print(\"Cumulative probability = {0:.3f}\".format(cdf))\n", |
562 | | - "print(\"Significance: {0:.3f}\".format(1 - cdf))" |
| 565 | + "print(f\"Cumulative probability = {cdf:.3f}\")\n", |
| 566 | + "print(f\"Significance: {1 - cdf:.3f}\")" |
563 | 567 | ] |
564 | 568 | }, |
565 | 569 | { |
|
690 | 694 | } |
691 | 695 | }, |
692 | 696 | "source": [ |
693 | | - "You can customize the layout with [`GridSpec`](https://matplotlib.org/users/gridspec.html) by using the the `subplot2grid` helper function." |
| 697 | + "You can customize the layout with [`GridSpec`](https://matplotlib.org/users/gridspec.html) by using the `subplot2grid` helper function." |
694 | 698 | ] |
695 | 699 | }, |
696 | 700 | { |
|
0 commit comments