a = 2 print("The value of a is", a) a = 2 print("The value of a is", a) b = a print("a =", b, "= b") quantity = 6 item = "bananas" price = 1.74 print(f"{quantity} {item} cost ${price}") print("{0} {1} cost ${2}".format(quantity, item, price)) print("%d %s cost $%.2f" % (6, "bananas", 1.74)) print("{0} {1} cost ${2}".format(6, "bananas", 1.74)) print("{quantity} {item} cost ${price}".format(quantity=6, item="bananas", price=1.74)) print("{2}.{1}.{0}/{0}{0}.{1}{1}.{2}{2}".format("foo", "bar", "baz")) print("{x}/{y}/{z}".format(x="foo", y="bar", z="baz")) a = 2 print("The value of a is", a, sep="") print("hello", "world", sep="\n") print("home", "user", "documents", sep="/") print(1, "Python Tricks", "Dan Bader", sep=",") print("node", "child", "child", sep=" -> ") print("Checking file integrity...", end="") # (...) print("ok") print("Mercury", "Venus", "Earth", sep=", ", end=", ") print("Mars", "Jupiter", "Saturn", sep=", ", end=", ") print("Uranus", "Neptune", "Pluto", sep=", ") print("12", "02", "2020", sep="-") print("12", "02", "2020", sep="-", end="\n\n\n") print("Hi!") print("12", "02", "2020", sep="-", end="\nto the future") print("Hi!") import math print(f"The value of pi is approximately {math.pi:.3f}.") print('We are the {} who say "{}!"'.format("knights", "Ni")) print("{0} and {1}".format("spam", "eggs")) print("{1} and {0}".format("spam", "eggs")) print( "This {food} is {adjective}.".format(food="spam", adjective="absolutely horrible") ) print("The story of {0}, {1}, and {other}.".format("Bill", "Manfred", other="Georg")) import os print("Hello, " + os.getlogin() + "! How are you?") print(f"Hello, {os.getlogin()}! How are you?") print("My name is", os.getlogin(), "and I am", 42) def get_name_and_decades(name, age): return f"My name is {name} and I'm {age / 10:.5f} decades old." print(get_name_and_decades("Maria", 31)) b = "Attaché case".encode() # Many different codecs are available print(b) print(b.decode()) print("{:g}".format(3.14159)) print("{:g}".format(-123456789.8765)) print("{:G}".format(-123456789.8765)) print("{0:<8s}".format("foo")) print("{0:<8d}".format(123)) print("{0:>8s}".format("foo")) print("{0:>8d}".format(123)) print("{0:^8s}".format("foo")) print("{0:^8d}".format(123)) print("{0:+8d}".format(123)) print("{0:=+8d}".format(123)) print("{0:+8d}".format(-123)) print("{0:=+8d}".format(-123)) print("{0:->8s}".format("foo")) print("{0:#<8d}".format(123)) print("{0:*^8s}".format("foo")) print("{0:+6d}".format(123)) print("{0:+6d}".format(-123)) print("{0:-6d}".format(123)) print("{0:-6d}".format(-123)) print("{0:*> 6d}".format(123)) print("{0:*>6d}".format(123)) print("{0:*> 6d}".format(-123)) print("{0:.0f}, {0:#.0f}".format(123)) print("{0:.0e}, {0:#.0e}".format(123)) print("{0:05d}".format(123)) print("{0:08.1f}".format(12.3)) print("{0:>06s}".format("foo")) print("{0:*>05d}".format(123)) print("{0:,d}".format(1234567)) print("{0:_d}".format(1234567)) print("{0:,.2f}".format(1234567.89)) print("{0:_.2f}".format(1234567.89)) print("{0:8.2f}".format(1234.5678)) print("{0:8.4f}".format(1.23)) print("{0:8.2e}".format(1234.5678)) print("{0:8.4e}".format(1.23)) print("{0:{grp}d}".format(123456789, grp="_")) print("{0:{grp}d}".format(123456789, grp=",")) w = 10 p = 2 print("%*.*f" % (w, p, 123.456)) # Width is 10, precision is 2 print("{2:{0}.{1}f}".format(w, p, 123.456)) print("{val:{wid}.{pr}f}".format(wid=w, pr=p, val=123.456)) print("{0:10.2f}".format(123.456)) x = 3 y = 7 print(f"The larger of {x} and {y} is {x if x > y else y}") age = 13 print(f'I am {"a minor" if age < 18 else "an adult"}.')