Strings Formatting in Python

Overview

Python supports the usage of format strings. There are different types of format specifiers, and every specifier acts as a placeholder for that type in the string.

The percent ‘%’ character marks the start of the specifier.

%s # used for strings
%d # used for numbers
%f # used for floating point
# Here we make use of the %d specifier since it's an integer
s= "there are %d items in the basket. " % 5

x = 'apple'
y = 'lemon'

# Since this contains more than one value, we will have to surround it with
#parenthesis.
z = "The items in the basket are %s and %s" % (x,y)

Note, on the Strings Introduction you can find more updated information about String formatting in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *