f-Strings
f-Strings అంటే ఏమిటి?
f-String అనేది Python లో text మరియు variable values ను సులభంగా కలిపి చూపించడానికి ఉపయోగించే string formatting విధానం.
String ముందు f లేదా F పెట్టి, variable ను curly brackets {} లో రాస్తే Python ఆ variable value ను string లోకి చేర్చుతుంది.
Syntax
f"Hello, {name}!"
ఇక్కడ f string formatting కోసం ఉపయోగించబడుతుంది. {name} లోని variable value అక్కడ కనిపిస్తుంది.
f-Strings ఎందుకు ఉపయోగించాలి?
- Text మరియు variables ను సులభంగా కలపవచ్చు.
- String concatenation కంటే code చిన్నగా, చదవడానికి సులభంగా ఉంటుంది.
- Numbers ను text తో కలిపేటప్పుడు type conversion అవసరం తక్కువగా ఉంటుంది.
- Calculated values ను కూడా string లో చూపించవచ్చు.
ఎలా పనిచేస్తుంది?
Python ఒక f-string ను చదివేటప్పుడు curly brackets లో ఉన్న expression లేదా variable ను evaluate చేసి, దాని value ను string లో ఉంచుతుంది.
ఉదాహరణకు, f"My age is {age}" లో age value string లో చేర్చబడుతుంది.
f-Strings లో expressions
Curly brackets లో variable మాత్రమే కాకుండా చిన్న calculation కూడా రాయవచ్చు. ఉదాహరణకు {price * quantity}. Python calculation చేసి వచ్చిన result ను string లో చూపిస్తుంది.
Formatting తో f-Strings
Decimal numbers ను నిర్దిష్ట decimal places తో చూపించడానికి {price:.2f} వంటి format specifier ఉపయోగించవచ్చు. ఇక్కడ .2f అంటే రెండు decimal places చూపించాలి అని అర్థం.
Simple Explanation
ఒక పేరు మరియు వయస్సు ఉన్నాయని అనుకుందాం. సాధారణంగా text మరియు variable ను కలపడానికి + ఉపయోగించాలి. కానీ f-string తో string ముందు f పెట్టి variable ను {} లో రాయడం చాలు.
name = "Ravi"
age = 20
print(f"My name is {name} and I am {age} years old.")
Python {name} స్థానంలో Ravi ను, {age} స్థానంలో 20 ను ఉంచుతుంది.
అందువల్ల f-Strings అనేవి dynamic messages, bills, reports మరియు user details చూపించడానికి చాలా ఉపయోగకరంగా ఉంటాయి.
Real-Life Example
ఒక shopping bill లో product name, quantity మరియు price చూపించాలి అనుకుందాం. ప్రతి value ను విడిగా print చేయకుండా, ఒకే f-string తో పూర్తి bill line తయారు చేయవచ్చు.
product = "Notebook"
quantity = 2
price = 45
print(f"{product}: {quantity} items, Total: {quantity * price}")
ఇక్కడ product పేరు, quantity మరియు calculation result ఒకే readable sentence లో కనిపిస్తాయి.
Code Examples
Variable values ను string లో చూపించడం
name = "Anu"
age = 21
print(f"My name is {name} and I am {age} years old.")
ఈ ఉదాహరణలో name మరియు age variables ను f-string లో ఉపయోగించాం. Curly brackets లో ఉన్న values ను Python స్వయంగా string లో చేర్చుతుంది.
My name is Anu and I am 21 years old.
Expression ను f-String లో ఉపయోగించడం
length = 10
width = 5
area = length * width
print(f"The area is {area} square units.")
మొదట length మరియు width తో area calculate చేశాం. తరువాత area variable ను f-string లో ఉంచి result ను sentence రూపంలో చూపించాం.
The area is 50 square units.
Decimal formatting తో f-String
price = 49.5
print(f"Price: {price:.2f}")
:.2f ఉపయోగించడం వల్ల price రెండు decimal places తో కనిపిస్తుంది. అందువల్ల 49.5 అనేది 49.50 గా output వస్తుంది.
Price: 49.50
Line-by-Line Explanation
ఈ line లో name అనే variable లో Anu అనే string value ను store చేస్తుంది.
ఈ line లో age అనే variable లో 21 అనే integer value ను store చేస్తుంది.
ఈ line f-string ను print చేస్తుంది. Python {name} స్థానంలో Anu ను, {age} స్థానంలో 21 ను ఉంచి పూర్తి sentence ను output చేస్తుంది.
ఈ line rectangle యొక్క length ను 10 గా store చేస్తుంది.
ఈ line rectangle యొక్క width ను 5 గా store చేస్తుంది.
ఈ line length మరియు width ను multiply చేసి area variable లో 50 ను store చేస్తుంది.
ఈ line f-string ద్వారా area value ను sentence లో చేర్చి output చేస్తుంది.
ఈ line price variable లో 49.5 అనే decimal value ను store చేస్తుంది.
ఈ line price ను f-string లో చూపిస్తుంది. :.2f format వల్ల decimal point తరువాత రెండు digits కనిపిస్తాయి.
Try It
Try this Python code:
name = "Kiran"
age = 18
print(f"My name is {name} and I am {age} years old.")
Practice
Practice: student_name మరియు marks అనే variables ఉపయోగించి, student పేరు మరియు marks ను f-string తో ఈ విధంగా చూపించండి: Meena scored 85 marks.
Hint: String ముందు f పెట్టండి. Variables ను curly brackets లో రాయండి.
Practice: length = 8 మరియు width = 4 values తో rectangle area calculate చేసి, f-string ద్వారా The area is 32 square units. అని చూపించండి.
Hint: మొదట area = length * width రాయండి. తరువాత area ను f-string లో ఉపయోగించండి.
Practice: price = 25.5 అనే value ను f-string ద్వారా రెండు decimal places తో చూపించండి.
Hint: Price: {price:.2f} అనే format ను ఉపయోగించండి.
Common Mistakes
String ముందు f పెట్టాలి: print(f”Hello, {name}”). అప్పుడు మాత్రమే Python variable value ను replace చేస్తుంది.
f-String లో variable కోసం curly brackets
{} మాత్రమే ఉపయోగించాలి.
Python case-sensitive. Variable ను తయారు చేసినప్పుడు ఉపయోగించిన spelling మరియు capitalization నే curly brackets లో ఉపయోగించాలి.
Opening quote మరియు closing quote రెండూ సరిగ్గా ఉండేలా చూసుకోండి. ఉదాహరణ: f”Hello, {name}”.
:.2f అంటే decimal point తరువాత రెండు digits చూపించడం అని గుర్తుంచుకోండి.
Interview Questions
f-String అంటే ఏమిటి?
Answer: f-String అనేది variables లేదా expressions ను string లో సులభంగా చేర్చడానికి ఉపయోగించే Python string formatting విధానం.
f-String ఎలా ప్రారంభమవుతుంది?
Answer: String ముందు చిన్న అక్షరం f లేదా పెద్ద అక్షరం F పెట్టాలి. ఉదాహరణ: f”Hello, {name}”.
f-String లో variable ను ఎలా చేర్చాలి?
Answer: Variable ను curly brackets లో రాయాలి. ఉదాహరణ: f”My age is {age}”.
f-String లో calculation చేయవచ్చా?
Answer: అవును. Curly brackets లో చిన్న expression రాయవచ్చు. ఉదాహరణ: f”Total: {price * quantity}”.
{price:.2f} లో .2f అర్థం ఏమిటి?
Answer: Decimal point తరువాత రెండు digits చూపించాలి అని .2f సూచిస్తుంది.
Exam Questions
క్రింది code యొక్క output ఏమిటి? name = “Ravi”; print(f”Hello, {name}!”)
Answer:
Output: Hello, Ravi!
f-String ఉపయోగించడానికి string ముందు ఏ అక్షరం పెట్టాలి?
Answer:
f లేదా F అక్షరం పెట్టాలి.
క్రింది code లో ఖాళీని పూరించండి: name = “Sita”; print(___”Welcome, {name}”)
Answer:
ఖాళీలో f రావాలి: print(f”Welcome, {name}”).
f-String లో values ను ఏ brackets లో రాయాలి?
Answer:
Curly brackets లేదా braces {} లో రాయాలి.
price = 10.5 ను రెండు decimal places తో చూపించే f-string రాయండి.
Answer:
print(f”{price:.2f}”)
Quick Revision
- f-String అనేది text మరియు variable values ను కలపడానికి ఉపయోగించే విధానం.
- String ముందు f లేదా F పెట్టాలి.
- Variables మరియు expressions ను curly brackets {} లో రాయాలి.
- f-String లో calculations కూడా రాయవచ్చు.
- :.2f ఉపయోగిస్తే decimal point తరువాత రెండు digits కనిపిస్తాయి.
- f-Strings code ను చిన్నగా మరియు readable గా చేస్తాయి.
