String Formatting


In order to compose strings out of other variables, python has a feature for this. The % operator is used to format a set of variables enclosed in a tuple (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", that are special symbols like %s and %d.

Let's say you have a variable called name with your user name in it, and you would then like to print out a greeting to that user.

# This prints out "Hello, John!"
name = "John"
print("Hello, %s!" % name)

To use two or more argument specifiers, use a tuple (parentheses):

# This prints out "John is 23 years old."
name = "John"
age = 23
print("%s is %d years old." % (name, age))

Any object which is not a string can be formatted using the %s operator as well. For example:

# This prints out: A list: [1, 2, 3]
mylist = [1,2,3]
print("A list: %s" % mylist)

Here are some basic argument specifiers you should know:

%s - String (or any object with a string representation, like numbers)

%d - Integers

%f - Floating point numbers

%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.

Exercise

You will need to write a format string which prints out the data using the following syntax: Hello John Doe. Your current balance is $53.40.

data = ("John", "Doe", 53.40) format_string = "Hello" print(format_string % data) data = ("John", "Doe", 53.40) format_string = "Hello %s %s. Your current balance is $%.2f." print(format_string % data) #test_output_contains("Hello John Doe. Your current balance is $53.40.", no_output_msg= "Make sure you add the `%s` in the correct spaces to the `format_string` to meet the exercise goals!") test_object('format_string', incorrect_msg="Make sure you add the `%s` in the correct spaces to the `format_string` to meet the exercise goals!") success_msg('Great work!')
Previous Tutorial Next Tutorial
Copyright © learnpython.org and LeineLab e.V.. Read our Terms of Use.