Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 877 Bytes

File metadata and controls

32 lines (26 loc) · 877 Bytes

String Formatting

Date: June 1, 2016

Basics

Given a String or Unicode object, the % operator (modulo) converts it to the specific format specified to its right.

print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height

The above code evaluates to

Let's talk about Zed A. Shaw.
He's 74 inches tall.

The first line of the above code formats my_name into String, while the second line converts my_height to a signed integer decimal.

Important to remember

print "So, you're %r old, %r tall and %r heavy." % (
	age, height, weight)

This code evaluates to

So, you're '38' old, '6\'2"' tall and '180lbs' heavy.

%r is used for debugging as it shows the raw representation of data while %s is used for displaying of data.

####References: