Python Strings: Indexing, Slicing, మరియు Methods
Cloud, DevOps, మరియు Automation కోసం Strings చాలా అవసరం. Logs, config files, మరియు API responses ప్రాసెస్ చేయడానికి ఇవి అవసరం.
String అనేది అక్షరాల యొక్క క్రమబద్ధమైన సమూహం (ordered collection). ఇది immutable (మార్చలేనిది). అంటే, ఒక string సృష్టించబడిన తర్వాత దానిని మీరు మార్చలేరు.
Indexing ప్రతి అక్షరానికి ఒక స్థానం (position) ఉంటుంది.
- Positive indexing ఎడమ వైపున 0 నుండి ప్రారంభమవుతుంది.
- Negative indexing కుడి వైపున -1 నుండి ప్రారంభమవుతుంది.
Example: text = "DevOps" text[0] అనేది D ని ఇస్తుంది. text[-1] అనేది s ని ఇస్తుంది.
Slicing Slicing ద్వారా మీరు [start:stop:step] ఫార్మాట్ను ఉపయోగించి string లోని భాగాలను వేరు చేయవచ్చు.
- Start index పరిగణనలోకి తీసుకోబడుతుంది.
- Stop index పరిగణనలోకి తీసుకోబడదు.
- Step అనేది ఐచ్ఛికం (optional).
Examples:
- "DevOps"[0:3] అనేది Dev ని ఇస్తుంది.
- "DevOps"[::2] అనేది Dvp ని ఇస్తుంది.
- "DevOps"[::-1] string ను రివర్స్ చేస్తుంది. Palindromes ని తనిఖీ చేయడానికి ఇది ఒక సాధారణ పద్ధతి.
Essential Methods
- len(text): మొత్తం అక్షరాల సంఖ్యను ఇస్తుంది.
- text.upper(): అన్నింటినీ uppercase గా మారుస్తుంది.
- text.lower(): అన్నింటినీ lowercase గా మారుస్తుంది.
- text.strip(): రెండు చివరల నుండి whitespace లేదా నిర్దిష్ట అక్షరాలను తొలగిస్తుంది.
- text.split(): string ను list గా మారుస్తుంది.
- " ".join(list): list ను తిరిగి string గా మారుస్తుంది.
- text.count("a"): "a" ఎన్నిసార్లు వచ్చిందో కనుగొంటుంది.
- text.find("a"): మొదటి "a" యొక్క index ని ఇస్తుంది. ఒకవేళ దొరకకపోతే -1 ని ఇస్తుంది.
ఈ చిన్న ఆపరేషన్లే పెద్ద automation scripts కు పునాది. క్లీన్ కోడ్ రాయడానికి వీటిని ప్రావీణ్యం సంపాదించండి.
Source: https://dev.to/tejas_shinkar/python-strings-indexing-slicing-and-essential-string-methods-3la0
Optional learning community: https://t.me/GyaanSetuAi