Python String Formatting Exercises


1/20
Which placeholder is used to insert an integer value when using the old-style % string formatting in Python?

In Python's old-style string formatting using the % operator:

  • %s is used for strings.
  • %d is used for integers.
  • %f is used for floating-point numbers.
  • %i can also be used for integers, but %d is the standard and widely preferred.

Example:

age = 25 print("I am %d years old" % age) # Output: I am 25 years old 

Key takeaway: Use %d for integers in old-style formatting, though modern Python prefers f-strings or .format().



About This Exercise: Python – String Formatting

String formatting is one of those skills in Python that you end up using almost everywhere, whether you’re writing simple scripts or working on larger applications. At Solviyo, we’ve created a complete set of Python string formatting exercises with explanations and answers to help you learn this topic in depth. Our approach is straightforward: instead of just showing you solutions, we walk you through the logic behind them, so you understand why the answer works.

We begin with the basics. You’ll practice simple formatting tasks such as inserting values into strings, aligning text, and controlling decimal places. These beginner-friendly exercises are designed to give you confidence with Python’s format() method as well as f-strings, which are now the most common way of formatting in modern Python. Each question comes with a detailed explanation and a correct answer, making sure you don’t just memorize but actually learn the concepts.

Once you’re comfortable, we move on to slightly more advanced formatting. This includes handling multiple variables, formatting numbers for readability, adding padding, working with percentages, and even formatting dates and times. These exercises reflect the kind of problems you’ll face in real-world projects—like displaying reports, generating user-friendly outputs, or handling currency values. Alongside these exercises, you’ll also find Python MCQs with answers and explanations to check your understanding quickly.

For learners who want to challenge themselves, we also include tricky formatting problems. These tasks involve combining text alignment with numeric formatting, nesting formatted strings, and exploring formatting options inside f-strings. You’ll even encounter scenarios where you need to dynamically build formats, which is especially useful in automation scripts. As always, every problem is supported with a clear explanation and answer to guide you through the thought process.

One of the things that make this topic so practical is how often it shows up in interviews and coding assessments. Employers want to see if you can present data neatly, generate reports, or make logs readable—and string formatting plays a big role in all of that. That’s why we’ve carefully mixed exercises and MCQs here at Solviyo: to give you both the hands-on coding practice and the quick checks that reinforce your knowledge.

By practicing consistently with our string formatting exercises, you’ll not only improve your coding style but also learn how to write cleaner and more efficient programs. Explore Solviyo’s set of Python string formatting exercises, explanations, and answers today, and take your Python skills to the next level.