Skip to content

Instantly share code, notes, and snippets.

@zziz
Created August 23, 2018 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zziz/e7559ae07cd67ecf2d430908fc0cad8d to your computer and use it in GitHub Desktop.
Save zziz/e7559ae07cd67ecf2d430908fc0cad8d to your computer and use it in GitHub Desktop.
Python formatted print
print("Dave have {} pens.".format(5))
Dave have 5 pens.
open_string = "Dave loves {}."
print(open_string.format("pepperoni pizza"))
Dave loves pepperoni pizza.
new_open_string = "Dave loves {} {}."                      
print(new_open_string.format("pepperoni", "pizza"))
Dave loves pepperoni pizza.
dave_string = "Dave loves {} {}, and has {} {}."                     
print(dave_string.format("pepperoni", "pizza", 5, "pens"))
Dave loves pepperoni pizza, and has 5 pens.
print("Dave the {} has a bike {}!".format("great", "tire"))
Dave the great has a bike tire!
print("Dave the {0} has a bike {1}!".format("great", "tire"))
Dave the great has a bike tire!
print("Dave the {1} has a bike {0}!".format("great", "tire"))
Dave the tire has a bike great!
print("Dave is a {}, {}, and {} {}!".format("tall", "skinny", "red", "ball"))
Dave is a tall, skinny, and red ball!
print("Dave is a {3}, {2}, and {1} {0}!".format("tall", "skinny", "red", "ball"))
Dave is a ball, red, and skinny tall!
print("Dave the {0} {1} a {pa}.".format("great", "called", pa = "police army"))
Dave the great called a police army.
print("Dave the {pa} {1} a {0}.".format("great", "called", pa = "police army"))
Dave the police army called a great.
print("Dave ate {0:f} percent of a {1}!".format(95, "pizza"))
Dave ate 95.000000 percent of a pizza!
print("Dave ate {0:.3f} percent of a pizza!".format(95.34567))
Dave ate 95.346 percent of a pizza!
print("Dave ate {0:.1f} percent of a pizza!".format(95.34567))
Dave ate 95.3 percent of a pizza!
print("Dave ate {0:.0f} percent of a pizza!".format(95.34567))
Dave ate 95 percent of a pizza!
print("Dave has {0:4} black {1:16}!".format(5, "pens"))
Dave has    5 black pens            !
print("Dave has {0:<4} black {1:^16}!".format(5, "pens"))
Dave has 5    black       pens      !
print("{:*^20s}".format("Dave"))
********Dave********
print("Dave ate {0:5.0f} percent of a pizza!".format(95.34567))
Dave ate    95 percent of a pizza!
nBalls = 8
print("Dave has {} pens today!".format(nBalls))
Dave has 8 pens today!
dave = "Dave has {} pens today!"
nBalls = 8
print(dave.format(nBalls))
Dave has 8 pens today!
for i in range(3,13):
    print(i, i*i, i*i*i)
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
11 121 1331
12 144 1728
for i in range(3,13):
    print("{:3d} {:4d} {:5d}".format(i, i*i, i*i*i))
  3    9    27
  4   16    64
  5   25   125
  6   36   216
  7   49   343
  8   64   512
  9   81   729
 10  100  1000
 11  121  1331
 12  144  1728
for i in range(3,13):
    print("{:6d} {:6d} {:6d}".format(i, i*i, i*i*i))
     3      9     27
     4     16     64
     5     25    125
     6     36    216
     7     49    343
     8     64    512
     9     81    729
    10    100   1000
    11    121   1331
    12    144   1728
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment