Searching Strings
Searching Strings అంటే ఏమిటి?
ఒక String లో ఏదైనా character లేదా word ఉందో లేదో తెలుసుకోవడాన్ని Searching Strings అంటారు.
Python లో String ను search చేయడానికి in, not in, find(), index(), startswith(), endswith(), count() వంటి methods మరియు operators ఉపయోగించవచ్చు.
Searching Strings ఎందుకు ఉపయోగిస్తాము?
- ఒక sentence లో word ఉందో లేదో తెలుసుకోవడానికి
- ఒక email లో
@ఉందో లేదో check చేయడానికి - ఒక file name ఏ extension తో ముగుస్తుందో తెలుసుకోవడానికి
- ఒక word ఎన్ని సార్లు వచ్చిందో లెక్కించడానికి
ముఖ్యమైన String Searching Tools
in: String లో character లేదా word ఉందా అని check చేస్తుంది.not in: String లో character లేదా word లేదో check చేస్తుంది.find(): వెతికిన text యొక్క మొదటి index ను ఇస్తుంది. దొరకకపోతే-1ఇస్తుంది.index(): వెతికిన text యొక్క మొదటి index ను ఇస్తుంది. దొరకకపోతే error వస్తుంది.startswith(): String ఒక ప్రత్యేక text తో ప్రారంభమవుతుందా అని check చేస్తుంది.endswith(): String ఒక ప్రత్యేక text తో ముగుస్తుందా అని check చేస్తుంది.count(): ఒక character లేదా word ఎన్ని సార్లు ఉందో లెక్కిస్తుంది.
ఎలా పనిచేస్తాయి?
Python String లో characters కు index numbers ఉంటాయి. Searching method ఒక text ను వెతికి, దాని స్థానం, ఉనికి, లేదా సంఖ్యను result గా ఇస్తుంది. in, startswith(), endswith() సాధారణంగా True లేదా False ఇస్తాయి. find() మరియు index() స్థానం ఇస్తాయి.
గమనించాల్సిన విషయం
String searching సాధారణంగా case-sensitive. అంటే Python మరియు python ఒకేలా పరిగణించబడవు.
Simple Explanation
ఒక పుస్తకంలో ఒక పదం ఉందో లేదో వెతికినట్లే, Python String లో కూడా word లేదా character ను వెతకవచ్చు.
in ఉపయోగిస్తే result True లేదా False వస్తుంది. find() ఉపయోగిస్తే text ఎక్కడ మొదలైందో index వస్తుంది. count() ఉపయోగిస్తే అది ఎన్నిసార్లు ఉందో తెలుస్తుంది.
ఉదాహరణకు "Python" in sentence అంటే sentence లో Python ఉందా అని అడుగుతుంది.
Real-Life Example
ఒక website login form లో user ఇచ్చిన email address ను check చేస్తున్నామని అనుకోండి. అందులో @ ఉందా అని in తో search చేయవచ్చు. అలాగే ఒక file name .pdf తో ముగుస్తుందా అని endswith() తో తెలుసుకోవచ్చు.
Code Examples
1. in మరియు not in తో search చేయడం
message = "Python is easy to learn."
print("Python" in message)
print("Java" in message)
print("Java" not in message)
in operator ఒక word String లో ఉందో లేదో check చేస్తుంది. not in ఆ word లేదో check చేస్తుంది. Result Boolean value అయిన True లేదా False గా వస్తుంది.
True False True
2. find() మరియు index() తో స్థానం కనుగొనడం
text = "Learn Python today"
print(text.find("Python"))
print(text.find("Java"))
print(text.index("Python"))
find() వెతికిన text మొదలయ్యే index ను ఇస్తుంది. Text దొరకకపోతే -1 ఇస్తుంది. index() కూడా index ను ఇస్తుంది, కానీ text దొరకకపోతే error వస్తుంది. అందువల్ల తెలియని text కోసం find() ఎక్కువగా సురక్షితమైన ఎంపిక.
6 -1 6
3. startswith(), endswith(), count() ఉపయోగించడం
filename = "report.pdf"
print(filename.startswith("report"))
print(filename.endswith(".pdf"))
print(filename.count("r"))
startswith() ప్రారంభ text ను check చేస్తుంది. endswith() చివరి text ను check చేస్తుంది. count() ఇచ్చిన character లేదా word ఎన్నిసార్లు ఉందో చెబుతుంది.
True True 2
Line-by-Line Explanation
ఈ line లో message అనే variable లో ఒక String నిల్వ అవుతుంది. తరువాత ఈ String లో words ను search చేయడానికి దీనిని ఉపయోగిస్తాము.
message లో Python ఉందా అని
in check చేస్తుంది. Python ఉన్నందున result True గా print అవుతుంది.
message లో Java ఉందా అని check చేస్తుంది. Java లేకపోవడంతో result
False అవుతుంది.
Java message లో లేదో
not in check చేస్తుంది. Java నిజంగా లేకపోవడంతో result True అవుతుంది.
text variable లో search చేయాల్సిన sentence నిల్వ అవుతుంది. Characters యొక్క index counting zero నుండి ప్రారంభమవుతుంది.
find() Python అనే word మొదలయ్యే index ను వెతుకుతుంది. P యొక్క index 6 కాబట్టి 6 print అవుతుంది.
Java అనే word ను వెతుకుతుంది. అది String లో లేకపోవడంతో
find() result గా -1 ఇస్తుంది.
index() Python word యొక్క మొదటి index ను ఇస్తుంది. Python index 6 నుండి ప్రారంభమవుతుంది కాబట్టి 6 print అవుతుంది.
filename variable లో file name String నిల్వ అవుతుంది. ఈ file name ప్రారంభం మరియు ముగింపును check చేస్తాము.
filename report అనే text తో ప్రారంభమవుతుందా అని check చేస్తుంది. ప్రారంభమవుతున్నందున
True వస్తుంది.
filename .pdf తో ముగుస్తుందా అని check చేస్తుంది. ముగింపు .pdf కావడంతో
True వస్తుంది.
filename లో r character ఎన్ని సార్లు ఉందో
count() లెక్కిస్తుంది. report లో r రెండు సార్లు ఉన్నందున 2 వస్తుంది.
Try It
Try this Python code:
sentence = "Python is powerful and Python is popular"
word = "Python"
print(word in sentence)
print(sentence.find(word))
print(sentence.count(word))
Practice
Practice: sentence = “Python is easy. Python is useful.” మరియు word = “Python” ఇచ్చారు. word sentence లో ఉందో check చేయండి, దాని మొదటి index ను కనుగొనండి, అలాగే word ఎన్ని సార్లు ఉందో count చేయండి.
Hint: in, find(), count() ఉపయోగించండి.
Practice: filename = “notes.txt” ఇచ్చారు. ఇది notes తో ప్రారంభమవుతుందా మరియు .txt తో ముగుస్తుందా అని check చేయండి.
Hint: startswith() మరియు endswith() ఉపయోగించండి.
Common Mistakes
find() text దొరకనప్పుడు 0 ఇస్తుందని అనుకోవడం.Text దొరకనప్పుడు
find() -1 ఇస్తుంది. 0 అంటే text String మొదటి index లో ఉందని అర్థం.
index() text దొరకనప్పుడు కూడా -1 ఇస్తుందని అనుకోవడం.index() text దొరకనప్పుడు error ఇస్తుంది. Text ఉండకపోవచ్చు అనుకుంటే find() ఉపయోగించండి.
String search case-sensitive.
"Python" మరియు "python" వేర్వేరు. Search text యొక్క case సరిపోవాలి.
startswith() ను String చివర check చేయడానికి లేదా endswith() ను ప్రారంభం check చేయడానికి ఉపయోగించడం.ప్రారంభం కోసం
startswith(), చివరి భాగం కోసం endswith() ఉపయోగించండి.
count() మొత్తం words సంఖ్యను ఇస్తుందని అనుకోవడం.count() మీరు ఇచ్చిన ప్రత్యేక character లేదా word ఎన్ని సార్లు ఉందో మాత్రమే లెక్కిస్తుంది.
Interview Questions
Python లో String లో ఒక word ఉందో లేదో ఎలా check చేస్తారు?
Answer: in operator ఉపయోగించవచ్చు. ఉదాహరణకు "Python" in text.
find() text దొరకనప్పుడు ఏమి return చేస్తుంది?
Answer: find() text దొరకనప్పుడు -1 return చేస్తుంది.
find() మరియు index() మధ్య తేడా ఏమిటి?
Answer: రెండూ text యొక్క మొదటి index ను ఇస్తాయి. కానీ find() text దొరకనప్పుడు -1 ఇస్తుంది; index() error ఇస్తుంది.
String ఒక particular extension తో ముగుస్తుందో లేదో ఎలా check చేస్తారు?
Answer: endswith() ఉపయోగించాలి. ఉదాహరణకు filename.endswith(".pdf").
Exam Questions
ఈ code output ఏమిటి? text = "Python"print("P" in text)
Answer:
True
ఈ code output ఏమిటి? text = "Hello"print(text.find("z"))
Answer:
-1
String మొదలవుతుందా అని check చేయడానికి ఏ method ఉపయోగిస్తారు?
Answer:
startswith() method.
ఒక word ఎన్ని సార్లు వచ్చిందో తెలుసుకోవడానికి ఏ method ఉపయోగిస్తారు?
Answer:
count() method.
Quick Revision
inString లో text ఉందో లేదో check చేసిTrueలేదాFalseఇస్తుంది.not intext లేదో check చేస్తుంది.find()text యొక్క మొదటి index ను ఇస్తుంది; దొరకకపోతే-1ఇస్తుంది.index()text యొక్క మొదటి index ను ఇస్తుంది; దొరకకపోతే error వస్తుంది.startswith()String ప్రారంభాన్ని check చేస్తుంది.endswith()String ముగింపును check చేస్తుంది.count()character లేదా word ఎన్నిసార్లు ఉందో లెక్కిస్తుంది.- String searching case-sensitive.
