6. String Formatting
📘 কনসেপ্ট (থিওরি)
স্ট্রিং ফরম্যাটিং আপনাকে স্ট্রিংয়ের ভেতরে ভেরিয়েবল বসাতে দেয়। পাইথনে এটি করার কয়েকটি উপায় আছে:
**f-strings (সবচেয়ে ভালো — Python 3.6+):**
সবচেয়ে আধুনিক ও পড়তে সহজ পদ্ধতি। স্ট্রিংয়ের আগে
f বসান এবং কার্লি ব্রেস {} এর মধ্যে ভেরিয়েবল দিন:
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
**format() মেথড:**
print("My name is {} and I am {} years old.".format(name, age))
**% অপারেটর (পুরোনো পদ্ধতি):**
print("My name is %s and I am %d years old." % (name, age))
f-string এ এক্সপ্রেশনও লেখা যায়:
print(f"Next year I will be {age + 1}")
💡 উদাহরণ
f-string দিয়ে সুন্দর আউটপুট:
product = "Laptop"
price = 75000
print(f"The {product} costs {price} taka.")
print(f"With 10% tax: {price * 1.1} taka.")
🎯 আপনার কাজ (প্র্যাকটিস)
city = "Dhaka" এবং population = 22000000 ভেরিয়েবল তৈরি করুন। f-string ব্যবহার করে প্রিন্ট করুন: Dhaka has a population of 22000000।main.py
Loading...
OUTPUT
Run your code to see the output here...