Watch Me Code 2: String Formatting


In [1]:
name = "Mike"
age = 45
salary = 15.75

In [2]:
# string formatting
print("Hello there %s. How are you? " % (name))


Hello there Mike. How are you? 

In [3]:
# formatting redux
print("%s makes %f per hour." % (name, salary))


Mike makes 15.750000 per hour.

In [6]:
# let's use spacing
print("%s makes %.2f per hour."  % (name, salary))


Mike makes 15.75 per hour.

In [7]:
# right alignment 
print("-" * 10) # print 10 dashes
print("%10d" %(age))


----------
        45

In [8]:
# left alignment
print("-" * 10)
print("%-10d" % (age))


----------
45        

In [ ]: