String అంటే ఏమిటి?
String అంటే ఏమిటి?
Pythonలో అక్షరాలు, పదాలు లేదా వాక్యాలను నిల్వ చేయడానికి ఉపయోగించే data type ను String అంటారు. ఉదాహరణకు ఒక వ్యక్తి పేరు, నగరం పేరు, సందేశం లేదా password వంటి text values అన్నీ strings కావచ్చు.
String ను Pythonలో సాధారణంగా single quotes లేదా double quotes మధ్య రాస్తారు. ఉదాహరణకు 'Python' మరియు "Python" రెండూ strings.
Strings ఎందుకు ఉపయోగిస్తాము?
- పేర్లు, చిరునామాలు, సందేశాలు వంటి text ను నిల్వ చేయడానికి.
- Screenపై text ను చూపించడానికి.
- User ఇచ్చే text data తో పని చేయడానికి.
- Text ను గుర్తించి ఉపయోగించడానికి.
String ఎలా పనిచేస్తుంది?
Quotes లో ఉన్న characters ను Python ఒకే text value గా పరిగణిస్తుంది. String ను ఒక variableలో నిల్వ చేసి, తరువాత ఆ variable పేరు ద్వారా ఉపయోగించవచ్చు.
Stringలో letters, numbers, spaces మరియు special characters ఉండవచ్చు. కానీ quotes లేకుండా రాస్తే Python దాన్ని stringగా గుర్తించదు.
Pythonలో String రాయడం
String ను single quotes లేదా double quotes మధ్య రాయాలి. ఒకే programలో రెండు రకాల quotes వాడవచ్చు, కానీ opening quote మరియు closing quote సరిపోలాలి.
name = "Ravi"
city = 'Hyderabad'
print(name)
print(city)
Ravi Hyderabad
ఇక్కడ name మరియు city variablesలో text values నిల్వ ఉన్నాయి. print() వాటిని screenపై చూపిస్తుంది.
Simple Explanation
సులభంగా చెప్పాలంటే, String అంటే text value. మనం ఒక text ను quotes మధ్య రాస్తే Python దాన్ని stringగా గుర్తిస్తుంది.
ఉదాహరణకు "Hello", 'Python', "123" అన్నీ strings. "123"లో 123 ఉన్నప్పటికీ అది text కాబట్టి string. Quotes string యొక్క ప్రారంభం మరియు ముగింపును సూచిస్తాయి.
String విలువను variableలో ఉంచి, అవసరమైనప్పుడు print చేయవచ్చు లేదా తరువాతి string operations కోసం ఉపయోగించవచ్చు.
Real-Life Example
Real-Life Example
ఒక పాఠశాల రిజిస్టర్లో విద్యార్థి పేరు, తరగతి పేరు, నగరం పేరు వ్రాసి ఉంటాయి. ఇవి సంఖ్యలు కాదు; ఇవి text. Pythonలో ఇలాంటి text ను Stringగా నిల్వ చేస్తాము.
ఉదాహరణకు ఒక విద్యార్థి పేరు "Anita" అయితే, Pythonలో దాన్ని student_name = "Anita"గా నిల్వ చేయవచ్చు.
Code Examples
String value ను variableలో నిల్వ చేయడం
name = "Anita"
print(name)
ఈ ఉదాహరణలో "Anita" అనే string value ను name variableలో నిల్వ చేస్తున్నాము. తరువాత print(name) ద్వారా ఆ stringను చూపిస్తున్నాము.
Anita
వేర్వేరు String values ను చూపించడం
language = 'Python'
message = "Learning is fun"
print(language)
print(message)
ఈ ఉదాహరణలో single quotes మరియు double quotes రెండింటితో strings రాయవచ్చని చూపించాము. రెండు string valuesను వేర్వేరు variablesలో నిల్వ చేసి print చేశాము.
Python Learning is fun
Stringలో numbers మరియు spaces
code = "Python 3"
print(code)
Stringలో lettersతో పాటు number మరియు space కూడా ఉండవచ్చు. "Python 3" మొత్తం ఒకే string value.
Python 3
Line-by-Line Explanation
ఈ lineలో "Anita" అనే text ను name variableలో నిల్వ చేస్తుంది. Quotes ఉండటం వల్ల Python దీనిని stringగా గుర్తిస్తుంది.
ఈ lineలో name variableలో ఉన్న string value ను screenపై చూపిస్తుంది. Python variableలో నిల్వ ఉన్న "Anita"ని outputగా ఇస్తుంది.
ఈ lineలో single quotes మధ్య ఉన్న 'Python' stringను language variableలో నిల్వ చేస్తుంది.
ఈ lineలో spacesతో ఉన్న పూర్తి text ను message variableలో stringగా నిల్వ చేస్తుంది.
ఈ lineలో language variableలో ఉన్న Python textను screenపై చూపిస్తుంది.
ఈ lineలో message variableలో ఉన్న Learning is fun textను screenపై చూపిస్తుంది.
ఈ lineలో letters, space మరియు number కలిసిన "Python 3" textను code variableలో stringగా నిల్వ చేస్తుంది.
ఈ lineలో code variableలో నిల్వ ఉన్న stringను screenపై చూపిస్తుంది.
Try It
Try this Python code:
favorite_language = "Python"
print(favorite_language)
Practice
Practice: ఒక విద్యార్థి పేరు మరియు అతని నగరం పేరును రెండు String variablesలో నిల్వ చేసి, రెండింటినీ print చేయండి.
Hint: Text valuesను quotes మధ్య రాయండి. ఉదాహరణకు student_name = "Anita".
Practice: మీకు నచ్చిన programming language ను ఒక String variableలో నిల్వ చేసి, దాన్ని print చేయండి.
Hint: ఒక variableను సృష్టించి దానికి quotes మధ్య language పేరు ఇవ్వండి.
Practice: "Python 3" అనే textను ఒక variableలో నిల్వ చేసి, దాన్ని outputగా చూపించండి.
Hint: Number ఉన్నప్పటికీ మొత్తం textను quotes మధ్య రాయాలి.
Common Mistakes
Quotes లేకుండా text రాయడం: name = Ravi. Python Raviను variableగా భావించి error ఇవ్వవచ్చు.
Text valueను quotes మధ్య రాయాలి: name = "Ravi".
Opening మరియు closing quotes సరిపోలకపోవడం: name = "Ravi'.
రెండు వైపులా ఒకే రకం quotes ఉపయోగించండి: name = "Ravi" లేదా name = 'Ravi'.
Stringలో ఉన్న numberను తప్పనిసరిగా numeric valueగా భావించడం. "123" ఒక string, ఎందుకంటే అది quotes మధ్య ఉంది.
Quotes మధ్య ఉంటే text string అవుతుంది. Numberగా ఉపయోగించాలంటే దాన్ని quotes లేకుండా రాయాలి, అయితే ఈ పాఠంలో stringsపై మాత్రమే దృష్టి పెట్టాలి.
Variableలో string నిల్వ చేసిన తరువాత, variable పేరు బదులు తప్పు పేరును print చేయడం.
విలువ నిల్వ చేసిన అదే variable పేరును print()లో ఉపయోగించండి.
Interview Questions
Pythonలో String అంటే ఏమిటి?
Answer: అక్షరాలు, పదాలు లేదా వాక్యాలను సూచించే text value ను String అంటారు. Pythonలో Stringను సాధారణంగా single quotes లేదా double quotes మధ్య రాస్తారు.
Pythonలో String ఎలా సృష్టిస్తారు?
Answer: Textను single quotes లేదా double quotes మధ్య రాయడం ద్వారా String సృష్టిస్తారు. ఉదాహరణ: name = "Ravi".
Single quotes మరియు double quotesతో String రాయవచ్చా?
Answer: అవును. 'Python' మరియు "Python" రెండూ valid strings. Opening మరియు closing quotes ఒకే రకంగా ఉండాలి.
"100" Stringనా?
Answer: అవును. Quotes మధ్య ఉన్నందున "100" ఒక String value.
Exam Questions
క్రింది వాటిలో String ఏది? A) 25 B) “25” C) 3.5
Answer:
సరైన సమాధానం B) "25". Quotes మధ్య ఉన్నందున అది String.
String valueను variableలో నిల్వ చేసే సరైన Python statement రాయండి.
Answer:
name = "Ravi"
క్రింది code output ఏమిటి? city = 'Delhi'
print(city)
Answer:
Output: Delhi
Stringలో spaces ఉండవచ్చా?
Answer:
అవును. ఉదాహరణకు "Good Morning"లో space ఉన్నప్పటికీ అది ఒకే String.
Quick Revision
- String అంటే text value.
- Stringను single quotes లేదా double quotes మధ్య రాయాలి.
- Opening మరియు closing quotes ఒకే రకంగా ఉండాలి.
- Stringను variableలో నిల్వ చేయవచ్చు.
- Stringలో letters, numbers, spaces మరియు special characters ఉండవచ్చు.
- Quotes మధ్య ఉన్న number కూడా Stringగా పరిగణించబడుతుంది.
- Stringను print చేయడానికి variableను
print()లో ఉపయోగించవచ్చు.
