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.
- Cutting and slicing strings in Python
- Python Programming – String Slices
- How to get and format the current time in Python
%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.