String Formatting

Python allows you to place variables within strings by using the "%" operator.

This is done like so:

name = "Jordan"

print("My name is %s" % name)

The %s is identifying where a variable will need to be and then the variable is assigned by placing it at the end, in the order that the variables should come up.

Like so:

name = "Jordan"

age = 16

print("I am %s and I am %d years old" % (name age))

* As we are using more than one variable we need to use a tuple once you start to identify the variables.

Argument Specifiers that you should know include:

%s - Strings

%d - Integers

%f - Floats

%. - Floating point numbers and a limit to how many decimals there are

%x/%X - Integers in hex

Continue to next lesson.