Replacing Strings
Replacing Strings అంటే ఏమిటి?
ఒక string లో ఉన్న పాత text ను కొత్త text తో మార్చడాన్ని Replacing Strings అంటారు. Python లో string ను replace చేయడానికి replace() method ను ఉపయోగిస్తాము.
ఎందుకు ఉపయోగిస్తాము?
Text లో spelling మార్చడానికి, పాత పేరు స్థానంలో కొత్త పేరు పెట్టడానికి, user data ను update చేయడానికి మరియు messages ను మార్చడానికి ఇది ఉపయోగపడుతుంది.
Syntax
string.replace(old, new)
old స్థానంలో మార్చాల్సిన text ఇవ్వాలి. new స్థానంలో పెట్టాల్సిన కొత్త text ఇవ్వాలి. ఈ method కొత్త string ను return చేస్తుంది.
ముఖ్యమైన విషయం
Python strings immutable. అంటే ఒక string ను నేరుగా మార్చలేము. replace() ఉపయోగించిన తర్వాత వచ్చిన కొత్త string ను ఒక variable లో store చేయాలి లేదా print చేయాలి.
replace() సాధారణంగా string లో కనిపించే అన్ని matching parts ను replace చేస్తుంది. ఇది uppercase మరియు lowercase ను వేర్వేరుగా పరిగణిస్తుంది. ఉదాహరణకు Python మరియు python ఒకేలా కావు.
Simple Explanation
ఒక sentence లోని ఒక word ను మరో word తో మార్చాలంటే replace() ఉపయోగించాలి.
ఉదాహరణకు, I like Java అనే string లో Java ను Python తో మార్చడానికి text.replace("Java", "Python") అని రాస్తాము.
ఈ method పాత string ను మార్చదు. బదులుగా కొత్త string ను తయారు చేస్తుంది. అందువల్ల result ను ఒక కొత్త variable లో store చేయడం మంచిది.
Real-Life Example
ఒక school notice లో పాత academic year 2024 అని ఉంది. కొత్త year 2025 కావాలి. మొత్తం notice ను తిరిగి రాయకుండా, replace() ఉపయోగించి 2024 స్థానంలో 2025 ను పెట్టవచ్చు. ఇదే విధంగా document లో company name లేదా product name మార్చడానికీ ఉపయోగించవచ్చు.
Code Examples
ఒక word ను మరో word తో replace చేయడం
text = "I like Java"
new_text = text.replace("Java", "Python")
print(new_text)
ఈ ఉదాహరణలో Java అనే పాత word ను Python అనే కొత్త word తో మార్చాము. వచ్చిన కొత్త string ను new_text variable లో store చేసి print చేశాము.
I like Python
ఒకే word పలుమార్లు ఉంటే replace చేయడం
message = "red red blue"
updated_message = message.replace("red", "green")
print(updated_message)
ఈ string లో red రెండు సార్లు ఉంది. replace() ఉపయోగించినప్పుడు matching అయిన రెండు places లోనూ green వస్తుంది.
green green blue
Line-by-Line Explanation
ఈ line లో text అనే variable లో ఒక string ను store చేస్తున్నాము. ఇందులో మార్చాల్సిన Java word ఉంది.
replace() method Java ను వెతికి, దాని స్థానంలో Python ను ఉంచుతుంది. వచ్చిన కొత్త string ను new_text లో store చేస్తుంది.
ఈ line లో replace చేసిన కొత్త string ను screen పై చూపిస్తుంది. అందువల్ల output గా I like Python వస్తుంది.
ఈ line లో message variable లో మూడు words ఉన్న string ను store చేస్తున్నాము. ఇందులో red రెండు సార్లు ఉంది.
ఈ line string లోని ప్రతి matching red word ను green తో replace చేస్తుంది. result ను updated_message లో store చేస్తుంది.
ఈ line లో మార్చబడిన string ను print చేస్తుంది. Output లో రెండు red words స్థానంలో రెండు green words కనిపిస్తాయి.
Try It
Try this Python code:
text = "Python is difficult"
updated_text = text.replace("difficult", "easy")
print(updated_text)
Practice
Practice: ఈ sentence లో ఉన్న ప్రతి Java word ను Python తో replace చేయండి: I study Java and Java is popular.
Hint: sentence.replace("Java", "Python") ను ఉపయోగించి result ను ఒక variable లో store చేయండి.
Practice: The color is blue. అనే string లో blue ను green తో replace చేయండి.
Hint: replace() లో మొదటి argument గా పాత word, రెండవ argument గా కొత్త word ఇవ్వండి.
Common Mistakes
replace() ఉపయోగించిన తర్వాత result ను store చేయకుండా పాత variable ను print చేయడం.
new_text = text.replace("old", "new") లాగా కొత్త result ను variable లో store చేయండి. లేదా అవసరమైతే నేరుగా print(text.replace("old", "new")) ఉపయోగించండి.
replace() లో ఇచ్చిన old text string లో ఉన్న text తో సరిగ్గా match కావాలి. Spelling మరియు spaces ను check చేయండి.
Python మరియు python వేర్వేరు. అవసరమైన exact case ను replace() లో ఇవ్వండి.
Python strings immutable. replace() కొత్త string ను return చేస్తుంది. కాబట్టి వచ్చిన result ను variable లో store చేయాలి.
replace() ను string variable పై dot notation తో ఉపయోగించండి. ఉదాహరణకు text.replace("old", "new").
Interview Questions
Python లో string text ను replace చేయడానికి ఏ method ఉపయోగిస్తారు?
Answer:
replace() method ను ఉపయోగిస్తారు.
replace() method యొక్క సాధారణ syntax ఏమిటి?
Answer:
string.replace(old, new) అనేది సాధారణ syntax.
replace() method original string ను మార్చుతుందా?
Answer:
లేదు. Python strings immutable కాబట్టి original string మారదు. replace() ఒక కొత్త string ను return చేస్తుంది.
ఒక string లో పాత word రెండు సార్లు ఉంటే ఏమవుతుంది?
Answer:
సాధారణంగా matching అయిన అన్ని occurrences replace అవుతాయి.
Exam Questions
క్రింది code output ఏమిటి? text = "Hello World"
print(text.replace("World", "Python"))
Answer:
Output: Hello Python
క్రింది code లో కొత్త string ఏ variable లో store అవుతుంది? name = "Sam"
new_name = name.replace("Sam", "Ram")
Answer:
కొత్త string new_name variable లో store అవుతుంది.
క్రింది code output ఏమిటి? word = "banana"
print(word.replace("a", "o"))
Answer:
Output: bonono
String లో matching text లేకపోతే replace() ఏమి చేస్తుంది?
Answer:
ఏ మార్పూ చేయకుండా original content లాంటి string ను return చేస్తుంది.
Quick Revision
replace()method string లోని text ను మార్చడానికి ఉపయోగిస్తారు.- Syntax:
string.replace(old, new) - మొదటి argument పాత text, రెండవ argument కొత్త text.
replace()కొత్త string ను return చేస్తుంది.- Original string మారదు, ఎందుకంటే Python strings immutable.
- Matching అయిన అన్ని occurrences సాధారణంగా replace అవుతాయి.
- Uppercase మరియు lowercase ను Python వేర్వేరుగా పరిగణిస్తుంది.
