String Formatting
String Formatting అంటే ఏమిటి?
ఒక text లో variables లేదా values ను సరైన స్థలంలో కలపడం మరియు అందంగా చూపించడం కోసం ఉపయోగించే విధానాన్ని String Formatting అంటారు.
ఉదాహరణకు, ఒక person’s name మరియు age ను ఒకే sentence లో చూపించాలంటే String Formatting ఉపయోగించవచ్చు.
String Formatting ఎందుకు ఉపయోగిస్తాము?
- Text మరియు variables ను సులభంగా కలపడానికి
- Output ను readable గా చూపించడానికి
- Numbers ను కావలసిన format లో చూపించడానికి
- Messages, bills, reports మరియు receipts తయారు చేయడానికి
Python లో String Formatting విధానాలు
ఈ lesson లో రెండు పద్ధతులను నేర్చుకుందాం:
- format() method
- % operator
format() method
String లో {} placeholders పెట్టి, చివరలో format() method ద్వారా values ఇస్తాము. Values ఇచ్చిన క్రమంలో placeholders లోకి వెళ్తాయి.
ఉదాహరణకు, మొదటి {} లో మొదటి value, రెండవ {} లో రెండవ value చేరుతుంది.
% operator
పాత-style formatting లో %s ను string కోసం, %d ను integer కోసం మరియు %f ను floating-point number కోసం ఉపయోగిస్తాము.
కొత్త Python code లో సాధారణంగా format() method లేదా తరువాతి lesson లో నేర్చుకునే f-Strings ఎక్కువగా ఉపయోగిస్తారు.
Simple Explanation
String Formatting అంటే ఒక sentence లో ఖాళీ స్థానాలను values తో నింపడం.
{} ను ఒక ఖాళీ స్థానం లాగా ఊహించుకోండి. format() లో ఇచ్చిన values ఆ ఖాళీ స్థానాలను నింపుతాయి.
ఉదాహరణకు, "Hello, {}".format(name) లో {} స్థానంలో name variable value వస్తుంది.
ఇలా చేస్తే string ను + తో అనేకసార్లు కలపాల్సిన అవసరం ఉండదు. Code చిన్నగా, స్పష్టంగా ఉంటుంది.
Real-Life Example
ఒక shop bill లో item name, quantity మరియు price చూపించాలి అనుకోండి. ప్రతి customer కు values మారుతాయి, కానీ sentence format ఒకటే ఉంటుంది. String Formatting ఉపయోగించి ఒకే template లో కొత్త values ను పెట్టవచ్చు.
ఉదాహరణకు: Product: Pen, Quantity: 2, Price: 20 అనే bill line ను formatting ద్వారా తయారు చేయవచ్చు.
Code Examples
format() method తో String Formatting
name = "Anu"
age = 20
message = "My name is {} and I am {} years old.".format(name, age)
print(message)
ఈ code లో {} placeholders ఉపయోగించబడ్డాయి. format(name, age) లోని values వరుస క్రమంలో placeholders లోకి వెళ్తాయి.
My name is Anu and I am 20 years old.
% operator తో String Formatting
name = "Ravi"
marks = 85
result = "{} scored %d marks." % marks
print(result.format(name))
ఈ code లో %d integer value కోసం ఉపయోగించబడింది. తరువాత format(name) ద్వారా మొదటి {} placeholder లో name value పెట్టబడుతుంది.
Ravi scored 85 marks.
Number formatting తో ధర చూపించడం
item = "Book"
price = 49.5
message = "Item: {}, Price: {:.2f}".format(item, price)
print(message)
{:.2f} అంటే floating-point number ను decimal point తరువాత రెండు digits తో చూపించాలి అని అర్థం. అందువల్ల 49.5 output లో 49.50 గా కనిపిస్తుంది.
Item: Book, Price: 49.50
Line-by-Line Explanation
ఈ line లో name అనే variable లో Anu అనే string value ను store చేస్తుంది. తరువాత ఈ value ను message లో ఉపయోగిస్తాము.
ఈ line లో age అనే variable లో 20 అనే integer value ను store చేస్తుంది.
ఈ line ఒక formatted string ను తయారు చేస్తుంది. మొదటి {} స్థానంలో name value, రెండవ {} స్థానంలో age value చేరుతుంది. తయారైన string message variable లో store అవుతుంది.
ఈ line message variable లో ఉన్న formatted text ను screen పై చూపిస్తుంది.
ఈ line name variable లో Ravi అనే string value ను store చేస్తుంది.
ఈ line marks variable లో 85 అనే integer value ను store చేస్తుంది.
ఈ line లో %d స్థానంలో marks value అయిన 85 చేరుతుంది. మొదటి {} placeholder ఇంకా అలాగే ఉంటుంది, ఎందుకంటే దాన్ని తరువాత format() తో నింపుతాము.
ఈ line result లోని మొదటి {} స్థానంలో name value అయిన Ravi ను పెట్టి, పూర్తి sentence ను print చేస్తుంది.
ఈ line item variable లో Book అనే product name ను store చేస్తుంది.
ఈ line price variable లో 49.5 అనే floating-point number ను store చేస్తుంది.
ఈ line item value ను మొదటి {} స్థానంలో పెడుతుంది. price value ను {:.2f} నియమం ప్రకారం decimal point తరువాత రెండు digits తో format చేస్తుంది.
ఈ line formatted item name మరియు price ను screen పై చూపిస్తుంది.
Try It
Try this Python code:
name = "Sita"
city = "Hyderabad"
message = "My name is {} and I live in {}.".format(name, city)
print(message)
Practice
Practice: item, quantity మరియు price variables ఉపయోగించి ఈ output వచ్చేలా String Formatting code రాయండి: Item: Pen, Quantity: 3, Total: 30.00
Hint: మొత్తం కోసం quantity * price ఉపయోగించండి. Total ను రెండు decimal digits తో చూపించడానికి {:.2f} ఉపయోగించండి.
Practice: name మరియు score variables ఉపయోగించి ఈ output వచ్చేలా code రాయండి: Student Ravi scored 90 marks.
Hint: String లో {} placeholders పెట్టి format(name, score) ఉపయోగించండి.
Common Mistakes
ఎన్ని {} placeholders ఉన్నాయో, వాటికి సరిపడే values ను format() లో ఇవ్వండి.
Formatting చేయాల్సిన string చివరలో .format() రాయండి. ఉదాహరణకు, “Hello, {}”.format(name).
{:.2f} floating-point number ను decimal point తరువాత రెండు digits తో చూపిస్తుంది.
%d integer values కోసం మాత్రమే ఉపయోగించండి. String value కోసం %s లేదా format() method ఉపయోగించండి.
Number ను string గా మార్చడానికి లేదా సులభంగా కలపడానికి format() method ఉపయోగించండి.
Interview Questions
String Formatting అంటే ఏమిటి?
Answer: Text లో variables లేదా values ను సరైన స్థానాల్లో పెట్టి readable output తయారు చేయడాన్ని String Formatting అంటారు.
Python లో format() method ను ఎలా ఉపయోగిస్తారు?
Answer: String లో {} placeholders పెట్టి, string చివరలో .format() method ద్వారా values ఇస్తారు. ఉదాహరణ: “Hello, {}”.format(name).
format() method లో values ఏ క్రమంలో placeholders లోకి వెళ్తాయి?
Answer: format() లో ఇచ్చిన మొదటి value మొదటి placeholder లోకి, రెండవ value రెండవ placeholder లోకి వెళ్తుంది.
{:.2f} అంటే ఏమిటి?
Answer: Floating-point number ను decimal point తరువాత రెండు digits తో చూపించాలి అని {:.2f} సూచిస్తుంది.
%d మరియు %s దేనికి ఉపయోగిస్తారు?
Answer: %d integer values కోసం, %s string values కోసం ఉపయోగిస్తారు.
Exam Questions
క్రింది code output ఏమిటి? name = “Rani”; print(“Hello, {}”.format(name))
Answer:
Output: Hello, Rani
format() method లో {} ను ఏమని పిలుస్తారు?
Answer:
{} ను placeholder అంటారు. ఇది value పెట్టే ఖాళీ స్థానాన్ని సూచిస్తుంది.
క్రింది code లో price output ఎలా కనిపిస్తుంది? price = 12.5; print(“Price: {:.2f}”.format(price))
Answer:
Output: Price: 12.50
String Formatting ఉపయోగించడం వల్ల ఒక ప్రయోజనం చెప్పండి.
Answer:
Variables మరియు text ను readable గా, తక్కువ code తో కలపవచ్చు.
format(“A”, “B”) ను “{} {}” string తో ఉపయోగిస్తే output ఏమిటి?
Answer:
Output: A B
Quick Revision
- String Formatting ద్వారా text లో variables మరియు values ను కలపవచ్చు.
- format() method లో {} placeholders ఉపయోగిస్తాము.
- format() లో values ఇచ్చిన క్రమంలో placeholders నిండుతాయి.
- {:.2f} floating-point number ను రెండు decimal digits తో చూపిస్తుంది.
- %s string కోసం, %d integer కోసం ఉపయోగించవచ్చు.
- Formatting వల్ల output readable గా ఉంటుంది.
- తరువాతి lesson లో f-Strings అనే మరొక ఆధునిక formatting విధానాన్ని నేర్చుకుంటాము.
