Practice question for output formatting



Question: Write a maximum of 5 lines of the python program describing yourself. Your program should have three variables declared (Ex. name, age, and height).
The expected output is attached below:

Output formatting question

Solution:

The above output formatting practice question can be solved in three different ways. All three types of formatting can be used as per own preference, however, each of them has its own advantage over the other. You can check in more detail about Output Formatting here.

Now, let’s look into three ways to solve the above question:

Sample Code 1: f-string method

name = "Dawa penjor"
age = 25
height = 5.8
print(f"Hello everyone! \nMy name is {name} and I am {age} years old.")
print(f"I am {height} feet tall.")

Sample Code 2: Manual formatting

name = "Dawa penjor"
age = 25
height = 5.8
print("Hello everyone! \nMy name is",name, "and I am",age,"years old.")
print("I am", height,"feet tall.")

Sample code 3: format() method

name = "Dawa penjor"
age = 25
height = 5.8
print("Hello everyone! \nMy name is {} and I am {} years old.".format(name, age))
print("I am {} feet tall.".format(height))

Output is the same for all the above three sample codes:

Hello everyone! 
My name is Dawa penjor and I am 25 years old.
I am 5.8 feet tall.