Strings
Python Strings
Pythonలో text లేదా అక్షరాల సమూహాన్ని String అంటారు. పేరు, నగరం, సందేశం, email address వంటి text dataను store చేయడానికి strings ఉపయోగిస్తాము.
String ఎలా రాయాలి?
Stringను single quotes లేదా double quotes లో రాయవచ్చు.
'Hello'"Hello"
Quotes లో ఉన్న textను Python ఒక string valueగా గుర్తిస్తుంది.
Strings ఎందుకు ఉపయోగిస్తాము?
వినియోగదారుడి పేరు, message, address, product name వంటి text సమాచారాన్ని store చేయడానికి మరియు display చేయడానికి strings ఉపయోగిస్తాము.
String operations
- Concatenation: రెండు stringsను
+తో కలపడం. - Repetition: ఒక stringను పలుమార్లు చూపించడానికి
*ఉపయోగించడం. - Length: stringలో ఎన్ని characters ఉన్నాయో తెలుసుకోవడానికి
len()ఉపయోగించడం. - Indexing: stringలోని ఒక characterను position ద్వారా తీసుకోవడం.
Pythonలో string indexing zero నుంచి ప్రారంభమవుతుంది. అంటే మొదటి character index 0, రెండవ character index 1.
Simple Explanation
String అంటే text. Stringను variableలో store చేసి తర్వాత print చేయవచ్చు.
ఉదాహరణకు, name = "Ravi" అని రాస్తే name variableలో Ravi అనే string store అవుతుంది. రెండు stringsను కలపడానికి + ఉపయోగిస్తాము. String length తెలుసుకోవడానికి len() ఉపయోగిస్తాము.
Stringలోని ప్రతి characterకు ఒక index ఉంటుంది. మొదటి character index 0 కావడం ముఖ్యమైన విషయం.
Real-Life Example
ఒక school ID cardలో student name మరియు class వంటి text information ఉంటుంది. Pythonలో వీటిని stringsగా store చేయవచ్చు. ఉదాహరణకు student name ఒక string, class name కూడా ఒక string. ఈ stringsను కలిపి ఒక messageగా చూపించవచ్చు.
Code Examples
Stringను store చేసి print చేయడం
name = "Ravi"
print(name)
ఇక్కడ name variableలో ఒక string store చేశాము. తర్వాత print() ద్వారా ఆ stringను screenపై చూపించాము.
Ravi
రెండు stringsను కలపడం
first_name = "Ravi"
last_name = "Kumar"
full_name = first_name + " " + last_name
print(full_name)
+ operator ఉపయోగించి రెండు stringsను కలిపాము. మధ్యలో ఖాళీ కోసం " " అనే stringను ఉపయోగించాము.
Ravi Kumar
String length మరియు indexing
word = "Python"
print(len(word))
print(word[0])
print(word[5])
len() stringలోని మొత్తం characters సంఖ్యను ఇస్తుంది. Indexing ద్వారా stringలోని నిర్దిష్ట characterను తీసుకోవచ్చు. మొదటి character index 0 నుంచి ప్రారంభమవుతుంది.
6 P n
String repetition
symbol = "*"
print(symbol * 5)
Stringను ఒక నిర్దిష్ట సంఖ్యలో repeat చేయడానికి * operatorను ఉపయోగించవచ్చు.
*****
Line-by-Line Explanation
Ravi అనే textను name variableలో store చేస్తుంది. Python ఈ valueను stringగా గుర్తిస్తుంది.
nameలో ఉన్న stringను screenపై చూపిస్తుంది.
Ravi అనే stringను first_name variableలో store చేస్తుంది.
Kumar అనే stringను last_name variableలో store చేస్తుంది.
+ operatorతో first name, ఒక ఖాళీ, last nameలను కలిపి పూర్తి పేరును full_nameలో store చేస్తుంది.
కలిపిన పూర్తి పేరును screenపై చూపిస్తుంది.
Python అనే stringను word variableలో store చేస్తుంది.
len(word) ద్వారా stringలోని characters సంఖ్యను లెక్కిస్తుంది. ఆ సంఖ్యను print చేస్తుంది.
word[0] ద్వారా stringలోని మొదటి characterను తీసుకుంటుంది. String index zero నుంచి ప్రారంభమవుతుంది కాబట్టి output P వస్తుంది.
word[5] ద్వారా ఆరో characterను తీసుకుంటుంది. Pythonలో ఆ character n.
* character ఉన్న stringను symbol variableలో store చేస్తుంది.
symbol stringను ఐదు సార్లు repeat చేసి screenపై చూపిస్తుంది.
Try It
Try this Python code:
city = "Hyderabad"
print(city)
print(len(city))
Practice
Practice: మీ first name మరియు last nameను రెండు variablesలో store చేసి, వాటిని కలిపి పూర్తి పేరుగా print చేయండి.
Hint: రెండు strings మధ్య ఖాళీ కోసం " " ఉపయోగించండి.
Practice: ఒక wordను variableలో store చేసి, దాని lengthను print చేయండి.
Hint: len() function ఉపయోగించండి.
Practice: ఒక symbolను ఐదు సార్లు print చేయండి.
Hint: String repetition కోసం * operator ఉపయోగించండి.
Common Mistakes
Stringను quotes లేకుండా రాయడం, ఉదాహరణకు name = Ravi.
Text valueను single లేదా double quotesలో రాయాలి: name = "Ravi".
String మరియు numberను నేరుగా +తో కలపడం, ఉదాహరణకు "Age: " + 20.
ఈ lessonలో stringsతో మాత్రమే concatenation చేయండి. Numberను stringగా మార్చే conversionను తర్వాత నేర్చుకోవచ్చు.
మొదటి character index 1 అని అనుకోవడం.
Pythonలో మొదటి character index ఎల్లప్పుడూ 0.
Stringలో లేని indexను ఉపయోగించడం, ఉదాహరణకు word = "Hi" తర్వాత word[5] రాయడం.
String lengthలో ఉన్న valid index మాత్రమే ఉపయోగించాలి. Hiకు valid indexes 0 మరియు 1.
Opening quote మరియు closing quote ఒకే రకంగా లేకపోవడం.
Single quote ప్రారంభిస్తే single quoteతోనే ముగించాలి లేదా double quoteతో ప్రారంభించి double quoteతో ముగించాలి.
Interview Questions
Pythonలో String అంటే ఏమిటి?
Answer:
Text లేదా characters సమూహాన్ని Pythonలో String అంటారు. ఉదాహరణ: "Hello".
Pythonలో Stringను ఎలా create చేస్తారు?
Answer:
Textను single quotes లేదా double quotesలో రాయడం ద్వారా Stringను create చేస్తారు. ఉదాహరణలు: 'Python', "Python".
రెండు stringsను ఎలా కలుపుతారు?
Answer:
+ operator ఉపయోగించి రెండు stringsను కలుపుతారు. దీనిని string concatenation అంటారు.
String length తెలుసుకోవడానికి ఏ function ఉపయోగిస్తారు?
Answer:
len() function ఉపయోగిస్తారు.
String indexing ఎక్కడి నుంచి ప్రారంభమవుతుంది?
Answer:
Pythonలో string indexing 0 నుంచి ప్రారంభమవుతుంది.
Exam Questions
క్రింది code output ఏమిటి? text = "Code"print(text[0])
Answer:
Output C. ఎందుకంటే మొదటి character index 0.
క్రింది code output ఏమిటి? a = "Py"b = "thon"print(a + b)
Answer:
Output Python. + operator రెండు stringsను కలుపుతుంది.
len("Hello") విలువ ఎంత?
Answer:
విలువ 5. ఎందుకంటే Helloలో ఐదు characters ఉన్నాయి.
Stringను మూడు సార్లు repeat చేయడానికి ఏ operator ఉపయోగిస్తారు?
Answer:
* operator ఉపయోగిస్తారు. ఉదాహరణ: "Hi" * 3.
Quick Revision
- String అంటే text లేదా characters సమూహం.
- Stringsను single quotes లేదా double quotesలో రాయాలి.
- రెండు stringsను కలపడానికి + operator ఉపయోగిస్తారు.
- String length తెలుసుకోవడానికి len() ఉపయోగిస్తారు.
- String indexing 0 నుంచి ప్రారంభమవుతుంది.
- Stringను repeat చేయడానికి * operator ఉపయోగించవచ్చు.
- Stringలో లేని indexను ఉపయోగించకూడదు.
