Splitting Strings
Splitting Strings అంటే ఏమిటి?
ఒక పెద్ద string ను చిన్న చిన్న భాగాలుగా విడగొట్టడాన్ని Splitting Strings అంటారు. Python లో string ను split చేయడానికి split() method ను ఉపయోగిస్తాము.
ఎందుకు ఉపయోగిస్తాము?
ఒక sentence లోని words ను విడిగా పొందడానికి, comma తో ఉన్న data ను items గా మార్చడానికి, user input ను భాగాలుగా విభజించడానికి ఇది ఉపయోగపడుతుంది.
ఎలా పనిచేస్తుంది?
split() method ఒక separator ఆధారంగా string ను విడగొట్టి, ఫలితంగా list ను ఇస్తుంది. Separator ఇవ్వకపోతే spaces ఆధారంగా string విడగొట్టబడుతుంది.
Syntax
string.split(separator)
ఉదాహరణకు, text.split() spaces ఆధారంగా విడగొడుతుంది. text.split(",") commas ఆధారంగా విడగొడుతుంది.
Simple Explanation
ఒక sentence ను ఒక పూర్తి string గా కాకుండా ప్రతి word గా విడిగా ఉపయోగించాలంటే split() అవసరం. ఉదాహరణకు "Python is easy" ను split చేస్తే ["Python", "is", "easy"] వస్తుంది.
ఇక్కడ ప్రతి word list లోని ఒక item అవుతుంది. Separator గా space, comma, hyphen వంటి characters ను ఉపయోగించవచ్చు.
Real-Life Example
ఒక shopping list “Book,Pen,Bag” రూపంలో ఉందనుకోండి. ప్రతి item ను విడిగా చూపించాలంటే comma ఆధారంగా string ను split చేయవచ్చు. అలాగే ఒక sentence లోని ప్రతి word ను విడిగా లెక్కించడానికి కూడా splitting ఉపయోగపడుతుంది.
Code Examples
Spaces ఆధారంగా String ను Split చేయడం
sentence = "Python is easy"
words = sentence.split()
print(words)
ఇక్కడ split() కు ఏ separator ఇవ్వలేదు. కాబట్టి Python spaces ఆధారంగా sentence ను విడగొట్టి words ను list గా తయారు చేస్తుంది.
['Python', 'is', 'easy']
Comma ఆధారంగా String ను Split చేయడం
items = "Book,Pen,Bag"
item_list = items.split(",")
print(item_list)
ఇక్కడ comma separator గా ఇచ్చాము. అందువల్ల comma కనిపించిన ప్రతి చోట string విడిపోయి, మూడు items list లో చేరతాయి.
['Book', 'Pen', 'Bag']
Hyphen ఆధారంగా String ను Split చేయడం
date = "2025-06-11"
parts = date.split("-")
print(parts)
ఇక్కడ date string ను hyphen ఆధారంగా విడగొట్టాము. ఫలితంగా year, month, day వేర్వేరు list items గా లభిస్తాయి.
['2025', '06', '11']
Line-by-Line Explanation
ఈ line లో sentence అనే variable లో ఒక string ను store చేస్తున్నాము. Python ఈ string ను memory లో ఉంచుతుంది.
ఈ line sentence string పై split() method ను అమలు చేస్తుంది. Separator ఇవ్వలేదు కాబట్టి spaces వద్ద string విడిపోయి, వచ్చిన words words అనే list లో store అవుతాయి.
ఈ line words list ను screen పై చూపిస్తుంది. అందువల్ల మూడు words list రూపంలో కనిపిస్తాయి.
ఈ line లో commaలతో ఉన్న shopping items string ను items variable లో store చేస్తున్నాము.
ఈ line comma ను separator గా ఉపయోగిస్తుంది. ప్రతి comma వద్ద string విడిపోయి, item_list అనే list తయారవుతుంది.
ఈ line split చేసిన items list ను output గా చూపిస్తుంది.
ఈ line లో hyphenలతో ఉన్న date string ను date variable లో store చేస్తున్నాము.
ఈ line hyphen ను separator గా తీసుకుని date ను మూడు భాగాలుగా విడగొడుతుంది. ఆ భాగాలు parts list లో store అవుతాయి.
ఈ line date లోని మూడు భాగాలను list రూపంలో screen పై చూపిస్తుంది.
Try It
Try this Python code:
text = "Python,Java,SQL"
parts = text.split(",")
print(parts)
Practice
Practice: message = “Learn Python Fast” అనే string ను spaces ఆధారంగా split చేసి, వచ్చిన list ను print చేయండి.
Hint: message.split() ను ఉపయోగించండి.
Practice: “Red|Green|Blue” అనే string ను pipe character | ఆధారంగా split చేసి list ను print చేయండి.
Hint: split("|") ను ఉపయోగించండి.
Common Mistakes
split అని parentheses లేకుండా రాయడం.split() అని parentheses తో రాయాలి. Method ను execute చేయడానికి parentheses అవసరం.
String లో ఉన్న separator ను గమనించి అదే character ను
split() లో ఇవ్వాలి.
split() సాధారణంగా strings ఉన్న list ను return చేస్తుందని గుర్తుంచుకోండి.
split(,).Separator ను string గా రాయాలి:
split(",").
Interview Questions
Python లో string ను split చేయడానికి ఏ method ఉపయోగిస్తారు?
Answer: split() method ను ఉపయోగిస్తారు.
split() method ఏ data type ను return చేస్తుంది?
Answer: split() method సాధారణంగా strings ఉన్న list ను return చేస్తుంది.
split() కు separator ఇవ్వకపోతే ఏమవుతుంది?
Answer: Python spaces ఆధారంగా string ను split చేస్తుంది.
"A,B,C".split(",") output ఏమిటి?
Answer: Output ['A', 'B', 'C'] అవుతుంది.
Exam Questions
క్రింది code యొక్క output ఏమిటి? text = "one two three"print(text.split())
Answer:
['one', 'two', 'three']
Comma ఆధారంగా string ను split చేయడానికి సరైన syntax ఏది?
Answer:
text.split(",")
క్రింది code యొక్క output ఏమిటి? value = "10-20-30"print(value.split("-"))
Answer:
['10', '20', '30']
split() method ను ఎందుకు ఉపయోగిస్తాము?
Answer:
ఒక string ను separator ఆధారంగా చిన్న భాగాలుగా విడగొట్టి list గా పొందడానికి ఉపయోగిస్తాము.
Quick Revision
split()ఒక string ను చిన్న భాగాలుగా విడగొడుతుంది.- Separator ఇవ్వకపోతే spaces ఆధారంగా split అవుతుంది.
- Comma కోసం
split(","), hyphen కోసంsplit("-")వంటివి ఉపయోగించాలి. - split() ఫలితంగా సాధారణంగా ఒక list వస్తుంది.
- String లో ఉన్న separator కు సరిపోయే character ను ఇవ్వాలి.
