Home / Programs / Format variables using a string.format() method.
🚀 Programming Example

Format variables using a string.format() method.

👁 393 Views
💻 Practical Program
📘 Step Learning

Write a python program to use string.format() method to format the following three variables as per the expected output

📌 Information & Algorithm

Given Input:

totalMoney = 1000
quantity = 3
price = 450

Expected Output:

I have 1000 dollars so I can buy 3 football for 450.00 dollars.

💻 Program Code

quantity = 3
totalMoney = 1000
price = 450
statement1 = "I have {1} dollars so I can buy {0} football for {2:.2f} dollars."
print(statement1.format(quantity, totalMoney, price))
                        

🖥 Program Output

I have 1000 dollars so I can buy 3 football for 450.00 dollars.
 
                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.