Python Format Exercises

Let’s check out some exercises that will help you understand .format() method better.

Exercise 1-a: Python format method of strings

Place a curly bracket "{}" in the appropriate place and fill inside .format()'s parenthesis so that "Hello!, Name" is printed.


Although it might look slightly weird in the beginning, .format() method is an extremely convenient method for string class objects.

The syntax is:
“{} {}”.format(value1, value2…)

As curly brackets get replaced with values inside the parenthesis.

str = "Hello!, {}".format(name)

 

Exercise 1-b: Format method to type a number

Using a string and .format() method print the number: 1 only.


Although it might look slightly weird in the beginning, .format() method is an extremely convenient method for string class objects.

The syntax is:
“{} {}”.format(value1, value2…)

As curly brackets get replaced with values inside the parenthesis.

print("{}".format(1))

Need More Exercises?

Check out Holy Python AI+ for amazing Python learning tools.

*Includes 14 more programming languages, inspirational tools and 1-on-1 consulting options.
Umut Sagir
Finance & Data Science Professional,
Founder of HolyPython