Practical String Programs
Practical String Programs
Stringలను కేవలం store చేయడమే కాకుండా, వాటిపై వివిధ operations చేయవచ్చు. ఉదాహరణకు stringను reverse చేయడం, vowels లెక్కించడం, palindrome గుర్తించడం, textను మార్చడం వంటి పనులు చేయవచ్చు.
String programs ఎందుకు ఉపయోగిస్తాము?
- వినియోగదారు ఇచ్చిన textను process చేయడానికి
- పేర్లు, messages, passwords వంటి text dataను పరిశీలించడానికి
- Textలో అక్షరాలు లేదా పదాలను వెతకడానికి
- String సరైన రూపంలో ఉందో లేదో తనిఖీ చేయడానికి
String program రాయడానికి సాధారణ విధానం
- Stringను variableలో store చేయాలి లేదా
input()ద్వారా తీసుకోవాలి. - అవసరమైన string operationను ఎంచుకోవాలి.
- ఫలితాన్ని మరో variableలో store చేసి print చేయాలి.
Simple Explanation
Practical string program అంటే stringపై ఒక ఉపయోగకరమైన పని చేసే చిన్న Python program.
ఉదాహరణకు text[::-1] stringను reverse చేస్తుంది. text.lower() అన్ని capital lettersను small lettersగా మార్చుతుంది. text.count("a") అనే codeలో a ఎన్ని సార్లు ఉందో లెక్కిస్తుంది.
ఒకే programలో string methods, loop, condition, indexing మరియు slicingను కలిపి ఉపయోగించవచ్చు. ఇలా చేయడం ద్వారా real-world text problemsను solve చేయగలుగుతాము.
Real-Life Example
ఒక libraryలో పుస్తకం పేరు Python Programmingగా ఉంది అనుకుందాం. ఆ పేరును reverseగా చూపించాలి, అందులో ఎన్ని vowels ఉన్నాయో తెలుసుకోవాలి లేదా అది palindromeనా కాదా అని తనిఖీ చేయాలి. ఇలాంటి text పనులను Python practical string programs ద్వారా చేయవచ్చు.
Code Examples
Program 1: Stringను Reverse చేయడం
text = "Python"
reversed_text = text[::-1]
print(reversed_text)
ఈ programలో slicing ఉపయోగించి stringలోని charactersను చివరి నుండి మొదటి వరకు చదివి reverse string తయారు చేస్తున్నాము.
nohtyP
Program 2: Stringలో Vowels లెక్కించడం
text = "education"
vowels = "aeiou"
count = 0
for character in text:
if character in vowels:
count = count + 1
print(count)
ఈ program stringలోని ప్రతి characterను పరిశీలిస్తుంది. అది vowel అయితే count విలువను ఒకటి పెంచుతుంది.
5
Program 3: Palindromeను తనిఖీ చేయడం
text = "level"
reversed_text = text[::-1]
if text == reversed_text:
print("Palindrome")
else:
print("Not a palindrome")
ముందుగా stringను reverse చేసి, original stringతో compare చేస్తున్నాము. రెండు strings ఒకేలా ఉంటే అది palindrome.
Palindrome
Line-by-Line Explanation
ఈ lineలో Python అనే stringను text variableలో store చేస్తాము. Programలో ఈ textను ఉపయోగించడానికి variable అవసరం.
ఇక్కడ slicing ద్వారా textను చివరి character నుండి మొదటి character వరకు చదువుతాము. అందువల్ల string reverse రూపంలో reversed_textలో store అవుతుంది.
ఈ line reversed stringను screenపై చూపిస్తుంది. Python execution సమయంలో nohtyP output వస్తుంది.
education అనే stringను text variableలో store చేస్తున్నాము. ఈ stringలో vowelsను లెక్కించబోతున్నాము.
లెక్కించవలసిన vowelsను ఒక stringగా vowels variableలో store చేస్తున్నాము. ఏ character vowelనా కాదా అని తనిఖీ చేయడానికి దీనిని ఉపయోగిస్తాము.
ప్రారంభంలో vowel count సున్నాగా ఉంచుతున్నాము. ప్రతి vowel కనిపించినప్పుడు ఈ విలువ ఒకటి పెరుగుతుంది.
ఈ loop textలోని ప్రతి characterను ఒక్కొక్కటిగా character variableలోకి తీసుకుంటుంది. అందువల్ల మొత్తం stringను పరిశీలించగలుగుతాము.
ప్రస్తుత character vowels stringలో ఉందో లేదో ఈ condition తనిఖీ చేస్తుంది. ఉంటే అది vowel అని అర్థం.
ప్రస్తుత character vowel అయితే count విలువను ఒకటి పెంచుతాము. ప్రతి vowelకు ఈ line execute అవుతుంది.
చివరగా మొత్తం vowels సంఖ్యను print చేస్తాము. educationలో 5 vowels ఉండటం వల్ల 5 output వస్తుంది.
level అనే stringను text variableలో store చేస్తున్నాము. ఇది palindromeనా కాదా అని తనిఖీ చేయబోతున్నాము.
textను slicing ద్వారా reverse చేసి reversed_textలో store చేస్తున్నాము. level reverse చేసినా levelగానే ఉంటుంది.
Original string మరియు reversed string సమానమా అని ఈ condition compare చేస్తుంది. సమానంగా ఉంటే string palindrome.
Condition True అయితే ఈ line execute అవుతుంది. String palindrome అని output చూపిస్తుంది.
if condition False అయినప్పుడు alternative blockను execute చేయడానికి else ఉపయోగిస్తాము.
Original string మరియు reversed string సమానం కాకపోతే ఈ message output అవుతుంది.
Try It
Try this Python code:
text = "hello world"
reversed_text = text[::-1]
print("Original:", text)
print("Reversed:", reversed_text)
Practice
Practice: వినియోగదారు ఒక wordను inputగా ఇచ్చినప్పుడు, అది palindromeనా కాదా అని చూపించే program రాయండి. ఉదాహరణకు level ఇస్తే Palindrome అని చూపించాలి.
Hint: ముందుగా slicingతో wordను reverse చేయండి. తరువాత original word మరియు reversed wordను == operatorతో compare చేయండి.
Practice: ఒక sentenceలో ఎన్ని vowels ఉన్నాయో లెక్కించే program రాయండి.
Hint: vowels = “aeiou” అనే string తీసుకుని, for loopతో ప్రతి characterను పరిశీలించండి.
Practice: ఒక stringలోని charactersను reverse orderలో print చేసే program రాయండి.
Hint: string slicingలో [::-1] ఉపయోగించండి.
Common Mistakes
Reverse చేయడానికి text[-1] మాత్రమే ఉపయోగించడం.
text[-1] చివరి characterను మాత్రమే ఇస్తుంది. మొత్తం stringను reverse చేయడానికి text[::-1] ఉపయోగించాలి.
Vowelsను తనిఖీ చేసేటప్పుడు character == vowels అని రాయడం.
ఒక character vowels stringలో ఉందో తెలుసుకోవడానికి character in vowels ఉపయోగించాలి.
Vowel count కోసం count = 1తో ప్రారంభించడం.
ప్రారంభంలో count ఎల్లప్పుడూ 0గా ఉండాలి. ప్రతి vowel దొరికినప్పుడు మాత్రమే ఒకటి పెంచాలి.
Palindrome తనిఖీలో original stringను reverse stringతో compare చేయకపోవడం.
if text == reversed_text: అనే comparison ఉపయోగించాలి.
Stringలో capital letters ఉన్నప్పుడు vowelsను సరిగ్గా లెక్కించకపోవడం.
ముందుగా text = text.lower() ఉపయోగిస్తే capital మరియు small letters రెండింటినీ సులభంగా తనిఖీ చేయవచ్చు.
Interview Questions
Pythonలో stringను reverse చేయడానికి సులభమైన విధానం ఏమిటి?
Answer:
Slicingలో text[::-1] ఉపయోగించాలి. ఇది stringను చివరి character నుండి మొదటి character వరకు చదువుతుంది.
Palindrome అంటే ఏమిటి?
Answer:
ముందు నుండి చదివినా, వెనుక నుండి చదివినా ఒకేలా ఉండే stringను palindrome అంటారు. ఉదాహరణకు level.
Stringలో ప్రతి characterను ఎలా పరిశీలించవచ్చు?
Answer:
for character in text: అనే for loop ఉపయోగించవచ్చు.
ఒక character vowelనా కాదా అని ఎలా తనిఖీ చేస్తారు?
Answer:
if character in "aeiou": అని తనిఖీ చేయవచ్చు.
Exam Questions
క్రింది code output ఏమిటి? text = "cat"print(text[::-1])
Answer:
Output: tac
Stringలో vowels లెక్కించడానికి ఉపయోగించే రెండు ముఖ్యమైన concepts ఏమిటి?
Answer:
Stringలోని ప్రతి characterను పరిశీలించడానికి for loop, vowelగా ఉందో తెలుసుకోవడానికి in operator ఉపయోగిస్తాము.
Palindrome checkలో stringను reverse చేయడం ఎందుకు అవసరం?
Answer:
Original string మరియు reverse string సమానంగా ఉన్నాయో తెలుసుకోవడానికి reverse చేయాలి. సమానంగా ఉంటే అది palindrome.
క్రింది codeలో count విలువ చివరికి ఎంత ఉంటుంది? text = "apple"count = text.count("p")
Answer:
count విలువ 2 ఉంటుంది, ఎందుకంటే appleలో p రెండు సార్లు ఉంది.
Quick Revision
- Practical string programs textపై ఉపయోగకరమైన పనులు చేస్తాయి.
text[::-1]stringను reverse చేస్తుంది.- Stringలోని ప్రతి characterను పరిశీలించడానికి
forloop ఉపయోగించవచ్చు. character in vowelsద్వారా character vowelనా కాదా తెలుసుకోవచ్చు.- Palindrome కోసం original stringను reversed stringతో compare చేయాలి.
- Count ప్రారంభ విలువను సాధారణంగా
0గా ఉంచాలి. - Capital మరియు small letters సమస్యను తగ్గించడానికి
lower()ఉపయోగించవచ్చు.
