>>> name = "John Smith" >>> username = "jsmith"
Hello, John Smith! Your username is jsmith
>>> print("Hello ", name, "! Your username is ", username)
Hello John Smith ! Your username is jsmith
>>> print("Hello, " + name + "! Your username is", username)
Hello, John Smith! Your username is jsmith
>>> print(f"Hello, {name}! Your username is {username}") Hello, John Smith! Your username is jsmith
>>> print(1/3) 0.3333333333333333
float value can hold>>> bill = 54.35 >>> print(bill, "split three ways is", bill/3) 54.35 split three ways is 18.116666666666667
round function
>>> print(bill, "split three ways is", round(bill/3, 2))
54.35 split three ways is 18.12
>>> print("The age of the Universe is 13800000000 years")
The age of the Universe is 13800000000 years
>>> print("The size of an electron is .0000000000000282 meters")
The size of an electron is .0000000000000282 meters
printf,
in these situations
printf stands for "print formated"
{LOOP_VARIABLE:FORMAT_SPECIFIER}
float
float is
f
float is .2f
>>> value = 12345.6789
>>> print(f"The value is {value}")
The value is 12345.6789
>>> print(f"The value is {value:.2f}")
The value is 12345.68
>>> print(f"The value is {value:.3f}")
The value is 12345.679
format function automatically rounds the
last digit ...
round function>>> universe_age = 13800000000 >>> print(f"The age of the universe is {universe_age:e} years") The age of the universe is 1.380000e+10 years >>> >>> print(f"The size of an electron is {electron_size:e} meters") The size of an electron is 2.820000e-14 meters
>>> print(f"The age of the universe is {universe_age:.2e} years") The age of the universe is 1.38e+10 years >>> print(f"The size of an electron is {electron_size:.2e} meters") The size of an electron is 2.82e-14 meters
>>> value = 12345.67890
>>> print(f"The value is {value:,.2f}")
The value is 12,345.68
>>> universe_age = 13800000000
>>> print(f"The age of the Universe is {universe_age:,d} years")
The age of the Universe is 13,800,000,000 years
$ cat print_07.py
# uses tabs to print a table
print("Mon\tTue\tWed\tThr\tFri")
print("15\t24\t89\t31\t86")
print("21\t79\t74\t23\t79")
$ python3 print_07.py
Mon Tue Wed Thr Fri
15 24 89 31 86
21 79 74 23 79
$ cat print_08.py
# uses tabs to print a table
print("Mon\tTue\tWed\tThr\tFri")
print("13452.12\t24\t234523.4589\t31\t86")
print("2324.1\t79\t74\t22343.453\t79")
$ python3 print_08.py
Mon Tue Wed Thr Fri
13452.12 24 234523.4589 31 86
2324.1 79 74 22343.453 79
$ cat print_09.py # # prints a table of values using f-strings print(f"{13452.12:10.2f} {24:10d} {234523.4589:10.2f} {31:10d} {86:10d}{24:10d} {234523.4589:10.2f} {31:10d} {86:10d}") print(f"{2324:10d} {79:10d} {74:10d} {22343.453:10.2f} {79:10d}" $ python3 print_09.py 13452.12 24 234523.46 31 86 2324 79 74 22343.45 79
$ cat print_10.py
# prints a table of values using format specifiers
print(f"{'Mon':10s} {'Tue':10s} {'Wed':10s} {'Thr':10s} {'Fri':10s}")
print(f"{13452.12:10.2f} {24:10d} {234523.4589:10.2f} {31:10d} {86:10d}")
print(f"{2324:10d} {79:10d} {74:10d} {22343.453:10.2f} {79:10d}")
$ python3 print_10.py
Mon Tue Wed Thr Fri
13452.12 24 234523.46 31 86
2324 79 74 22343.45 79
$ cat print_11.py
# prints a table of values using format specifiers
print(f"{'Mon':>10s} {'Tue':>10s} {'Wed':>10s} {'Thr':>10s} {'Fri':>10s}")
print(f"{13452.12:10.2f} {24:10d} {234523.4589:10.2f} {31:10d} {86:10d}")
print(f"{2324:10d} {79:10d} {74:10d} {22343.453:10.2f} {79:10d}"
$ python3 print_11.py
Mon Tue Wed Thr Fri
13452.12 24 234523.46 31 86
2324 79 74 22343.45 79
>>> print(f"3 out of 10 is {3/10:.0%}")
3 out of 10 is 30%
[alignment] [width] [,] [.precision] [type]