পাইথন - স্ট্রিং ফরম্যাটিং
পাইথন ভেরিয়েবল অধ্যায়ে আমরা যেমন শিখেছি, স্ট্রিং এবং সংখ্যা এইভাবে সংযুক্ত করা যাবে না:
Error Example
age = 36
# This will produce an error:
txt = "My name is John, I am " + age
print(txt)
# TypeError: can only concatenate str to str
কিন্তু আপনি f-স্ট্রিং বা ফরম্যাট() পদ্ধতি ব্যবহার করে স্ট্রিং এবং সংখ্যা সংযুক্ত করতে পারেন!
F-Strings
F-স্ট্রিং কি?
F-String Python 3.6-এ চালু করা হয়েছিল এবং এখন স্ট্রিং ফর্ম্যাট করার পছন্দের উপায়।
একটি স্ট্রিংকে f-স্ট্রিং হিসাবে নির্দিষ্ট করতে, স্ট্রিং ধ্রুবকের সামনে f রাখুন এবং ভেরিয়েবল এবং অন্যান্য ফাংশনের স্থান হিসাবে কোঁকড়া বন্ধনী {} যোগ করুন।
F-স্ট্রিং বিন্যাস
একটি পরিবর্তনশীল বয়স = 36 { } এ ঢোকানো হয়
F-String Example
# Create an f-string
age = 36
txt = f"My name is John, I am {age}"
print(txt) # Output: My name is John, I am 36
স্থানান্তর এবং রূপান্তরকারী
একটি অ্যাসাইনমেন্টে মান বিন্যাস করার জন্য ভেরিয়েবল, ফাংশন, নির্ভরতা এবং মডিফায়ার থাকতে পারে।
মৌলিক ইনপুট
Basic Placeholder Example
# Add a placeholder for the price variable
price = 59
txt = f"The price is {price} dollars"
print(txt) # Output: The price is 59 dollars
মডিফায়ার সহ স্থানান্তর
একটি সংশোধক যোগ করা হয়েছে : একটি আইনি বিন্যাস টাইপ অনুসরণ করে, .2f 2 দশমিক স্থান সহ একটি নির্দিষ্ট-বিন্দু সংখ্যা উপস্থাপন করে:
কনভার্টার ডিজাইন
.2f রূপান্তরকারী 2 দশমিক স্থানে ফর্ম্যাট করে
Format Modifier Example
# Display the price with 2 decimals
price = 59
txt = f"The price is {price:.2f} dollars"
print(txt) # Output: The price is 59.00 dollars
আমানত উপর অপারেশন
একটি আমদানিতে পাইথন কোড থাকতে পারে, যেমন গণিত অপারেশন:
Operations in Placeholders Example
# Perform a math operation in the placeholder
txt = f"The price is {20 * 59} dollars"
print(txt) # Output: The price is 1180 dollars
# Complex expressions
a = 10
b = 20
txt = f"The sum is {a + b} and product is {a * b}"
print(txt) # Output: The sum is 30 and product is 200
# Function calls
name = "john"
txt = f"Name in uppercase: {name.upper()}"
print(txt) # Output: Name in uppercase: JOHN
# Multiple operations
items = 5
price_per_item = 12.5
txt = f"Total: ${items * price_per_item:.2f}"
print(txt) # Output: Total: $62.50
এফ-স্ট্রিং পাওয়ার:
F-স্ট্রিংগুলি একটি স্ট্রিংয়ের ভিতরে যেকোন বৈধ পাইথন এক্সপ্রেশনকে কার্যকর করার অনুমতি দেয়, যা তাদের খুব শক্তিশালী করে তোলে।
সাধারণ বিন্যাস সংশোধক
| কনভার্টার | ব্যাখ্যা | উদাহরণ | প্রকাশনা |
|---|---|---|---|
:.2f |
2 দশমিক স্থান সহ একটি ফ্লোট সংখ্যা | f"{3.14159:.2f}" |
3.14 |
:.0f |
দশমিক ছাড়া একটি ভাসমান সংখ্যা | f"{3.14159:.0f}" |
3 |
:d |
পূর্ণসংখ্যা বিন্যাস | f"{42:d}" |
42 |
:x |
ষোল নম্বর (ছোট হাতের) | f"{255:x}" |
ff |
:X |
ষোল নম্বর (ক্যাপিটাল লেটার) | f"{255:X}" |
FF |
:b |
বাইনারি নং | f"{5:b}" |
101 |
:o |
অষ্ট নং | f"{8:o}" |
10 |
:, |
হাজার বিভাজক | f"{1000000:,}" |
1,000,000 |
:<10 |
বাম প্রান্তিককরণ (10 স্থান) | f"{'Hi':<10}" |
Hi |
:>10 |
ডান প্রান্তিককরণ (10 স্থান) | f"{'Hi':>10}" |
Hi |
:^10 |
কেন্দ্রীয় প্রান্তিককরণ (10 আসন) | f"{'Hi':^10}" |
Hi |
:% |
শতাংশ বিন্যাস | f"{0.25:%}" |
25.000000% |
ব্যায়াম - একটি F-স্ট্রিং ডিজাইন করা
যদি x = 9 হয়, তাহলে 'দাম 9.00 ডলার' প্রিন্ট করার জন্য সঠিক সিনট্যাক্স কী?
সঠিক সিনট্যাক্স চয়ন করুন:
বর্ণনা:
.2f সংশোধক x মানকে 2 দশমিক স্থান সহ ফর্ম্যাট করে
ফরম্যাট() পদ্ধতি
F-স্ট্রিং এর আগে, ফরম্যাট() পদ্ধতি ব্যাপকভাবে ব্যবহৃত হত:
ফরম্যাট() পদ্ধতি
# Using format() method
name = "John"
age = 36
txt = "My name is {}, I am {}".format(name, age)
print(txt) # Output: My name is John, I am 36
# With index positions
txt = "My name is {0}, I am {1}".format(name, age)
print(txt) # Output: My name is John, I am 36
# With named parameters
txt = "My name is {n}, I am {a}".format(n=name, a=age)
print(txt) # Output: My name is John, I am 36
F-স্ট্রিং (আধুনিক পদ্ধতি)
# Using f-string (modern way)
name = "John"
age = 36
txt = f"My name is {name}, I am {age}"
print(txt) # Output: My name is John, I am 36
# With expressions
txt = f"My name is {name.upper()}, I am {age + 5}"
print(txt) # Output: My name is JOHN, I am 41
F-String vs format():
F-স্ট্রিংগুলি পড়তে সহজ, লিখতে সহজ এবং ফর্ম্যাট() পদ্ধতির চেয়ে দ্রুত। নতুন কোডের জন্য সর্বদা f-স্ট্রিং ব্যবহার করুন।
উন্নত নকশা
কম্পোজিট মডিফায়ার
# Combine multiple modifiers
number = 1234567.8912
# Currency with 2 decimals and thousand separators
print(f"${number:,.2f}") # Output: $1,234,567.89
# Percentage with 1 decimal
percentage = 0.856
print(f"{percentage:.1%}") # Output: 85.6%
# Scientific notation
print(f"{number:.2e}") # Output: 1.23e+06
# Different width and alignment
name = "Python"
print(f"|{name:<20}|") # Output: |Python |
print(f"|{name:>20}|") # Output: | Python|
print(f"|{name:^20}|") # Output: | Python |
শর্তাধীন অভিব্যক্তি
# Conditional expressions in f-strings
score = 85
grade = f"Score: {score} - {'Pass' if score >= 50 else 'Fail'}"
print(grade) # Output: Score: 85 - Pass
# Multiple conditions
temperature = 22
feeling = f"It feels {'hot' if temperature > 30 else 'warm' if temperature > 20 else 'cool'}"
print(feeling) # Output: It feels warm
# Formatting based on value
value = 42
formatted = f"Value: {value:0>5}" # Pad with zeros to width 5
print(formatted) # Output: Value: 00042
ব্যবহারিক উদাহরণ
চালান
item = "Python Book"
price = 29.99
quantity = 3
total = price * quantity
invoice = f"""
Item: {item}
Price: ${price:.2f}
Quantity: {quantity}
Total: ${total:.2f}
"""
ডেটা রিপোর্টিং
users = 1500
growth = 0.25
report = f"""
Total Users: {users:,}
Growth Rate: {growth:.1%}
Projected: {int(users * (1 + growth)):,}
"""
ব্যবহারকারীর প্রোফাইল
name = "John Doe"
age = 30
salary = 75000.50
profile = f"""
Name: {name:>15}
Age: {age:>16}
Salary: ${salary:>12,.2f}
"""
দীপকের খবর
variable = "test"
value = 42.123456
debug = f"[DEBUG] {variable}={value:.3f}"
# [DEBUG] test=42.123
পুরানো স্ট্রিং শেপিং পদ্ধতি
| পদ্ধতি | উদাহরণ | ব্যাখ্যা | সুপারিশ |
|---|---|---|---|
| % অপারেটর | "Name: %s, Age: %d" % ("John", 30) |
একটি খুব পুরানো পদ্ধতি, সি থেকে ধার করা | এড়িয়ে চলুন |
| str.format() | "Name: {}, Age: {}".format("John", 30) |
Python 2.6+ | পুরানো কোডের জন্য |
| f-string | f"Name: {'John'}, Age: {30}" |
Python 3.6+ | প্রস্তাবিত |
স্ট্রিং বিন্যাস সম্পর্কে আরও জানুন:
আমাদের স্ট্রিং বিন্যাস সম্পর্কে আরও জানুনString Formattingঅধ্যায় দেখুন.