Single and Double Quotes
Single and Double Quotes
Python లో string ను రాయడానికి single quotes లేదా double quotes ఉపయోగించవచ్చు.
Single quotes కోసం ' ' మరియు double quotes కోసం " " ఉపయోగిస్తాము.
రెండు విధానాలు కూడా string ను తయారు చేస్తాయి. మీ code లో ఒకే విధానాన్ని consistent గా ఉపయోగించడం మంచిది.
Single Quotes
String ను single quotes మధ్య రాయవచ్చు.
Double Quotes
String ను double quotes మధ్య కూడా రాయవచ్చు.
ఎప్పుడు ఉపయోగించాలి?
- సాధారణ text కోసం single quotes లేదా double quotes ఏదైనా ఉపయోగించవచ్చు.
- String లో apostrophe ఉంటే double quotes ఉపయోగించడం సులభం.
- String లో double quote ఉంటే single quotes ఉపయోగించడం సులభం.
Opening quote మరియు closing quote ఒకే రకంగా ఉండాలి. ఉదాహరణకు, single quote తో ప్రారంభిస్తే single quote తోనే ముగించాలి.
Simple Explanation
Python కి string ఎక్కడ మొదలై ఎక్కడ ముగుస్తుందో quotes ద్వారా తెలుస్తుంది.
'Python' మరియు "Python" రెండూ ఒకే text ను సూచిస్తాయి.
String లో apostrophe ఉన్నప్పుడు "I am happy" వంటి double quotes ఉపయోగించవచ్చు. అలాగే string లో double quote ఉంటే single quotes ఉపయోగించవచ్చు.
ఒకే string కోసం opening మరియు closing quotes వేర్వేరుగా ఉంటే SyntaxError వస్తుంది.
Real-Life Example
ఒక వ్యక్తి చెప్పిన మాటలను రాస్తున్నప్పుడు బయట గుర్తులు మరియు లోపల ఉన్న మాటలు వేర్వేరుగా కనిపించాలి. Python లో కూడా ఒక quote ను చూపించడానికి బయట మరో రకమైన quote ఉపయోగించవచ్చు.
ఉదాహరణకు, ఒక వ్యక్తి Python is easy అని చెప్పాడని చూపించడానికి double quotes బయట, text కోసం single quotes లోపల ఉపయోగించవచ్చు.
Code Examples
Single Quotes తో String
message = 'Hello, Python!'
print(message)
ఇక్కడ string ను single quotes మధ్య రాశాము. ఆ string ను message variable లో store చేసి, తరువాత print చేశాము.
Hello, Python!
Double Quotes తో String
message = "Hello, Python!"
print(message)
ఇదే string ను double quotes మధ్య రాశాము. Single quotes మరియు double quotes రెండూ string తయారు చేయడానికి ఉపయోగపడతాయి.
Hello, Python!
String లో Apostrophe మరియు Double Quote
sentence = "I am learning Python."
quote = 'Python is easy.'
print(sentence)
print(quote)
మొదటి string కోసం double quotes, రెండవ string కోసం single quotes ఉపయోగించాము. రెండు strings ను వేర్వేరు variables లో store చేసి print చేశాము.
I am learning Python. Python is easy.
వేరే Quote ను String లో ఉపయోగించడం
message = "It's a sunny day."
print(message)
String లో It's అనే apostrophe ఉంది. అందువల్ల బయట double quotes ఉపయోగించాము. ఇలా చేస్తే Python string ముగిసిందని తప్పుగా అనుకోదు.
It's a sunny day.
Line-by-Line Explanation
Single quotes మధ్య ఉన్న text ను Python string గా గుర్తిస్తుంది. ఆ string ను message variable లో store చేస్తుంది.
message variable లో ఉన్న string ను screen పై చూపిస్తుంది.
Double quotes మధ్య ఉన్న text ను Python string గా గుర్తిస్తుంది. ఆ string ను message variable లో store చేస్తుంది.
message లో ఉన్న text ను output గా చూపిస్తుంది.
Double quotes ఉపయోగించి ఒక sentence string ను sentence variable లో store చేస్తుంది.
Single quotes ఉపయోగించి మరో string ను quote variable లో store చేస్తుంది.
sentence variable లో ఉన్న text ను మొదటి output line గా చూపిస్తుంది.
quote variable లో ఉన్న text ను తరువాతి output line గా చూపిస్తుంది.
String లో apostrophe ఉన్నందున బయట double quotes ఉపయోగించాము. Python మొత్తం sentence ను ఒకే string గా గుర్తిస్తుంది.
message variable లో ఉన్న apostrophe కలిగిన sentence ను output గా చూపిస్తుంది.
Try It
Try this Python code:
first_message = 'Python uses strings.'
second_message = "Quotes define a string."
print(first_message)
print(second_message)
Practice
Practice: ఒక program రాయండి. ఒక string ను single quotes తో, మరొక string ను double quotes తో create చేయండి. రెండు strings ను print చేయండి.
Hint: రెండు variables తయారు చేసి, ఒకదానికి single quotes మరియు మరొకదానికి double quotes ఉపయోగించండి.
Practice: Apostrophe ఉన్న It's a good day. అనే sentence ను Python string గా create చేసి print చేయండి.
Hint: బయట double quotes ఉపయోగించండి.
Common Mistakes
Opening quote మరియు closing quote ఒకే రకంగా ఉండకపోవడం. ఉదాహరణకు message = 'Hello".
Single quote తో ప్రారంభిస్తే single quote తోనే ముగించండి: message = 'Hello'. లేదా రెండు చోట్లా double quotes ఉపయోగించండి.
Apostrophe ఉన్న string ను single quotes తో రాయడం: message = 'It's sunny'.
బయట double quotes ఉపయోగించండి: message = "It's sunny".
String value చుట్టూ quotes పెట్టకుండా text రాయడం.
Text ను single లేదా double quotes మధ్య రాయండి. ఉదాహరణకు name = 'Ravi'.
ఒక string లో అవసరం లేని విధంగా quote types మార్చడం.
ఒక string కోసం opening మరియు closing quotes ఒకే రకంగా ఉండేలా చూసుకోండి.
Interview Questions
Python లో string ను ఏ రెండు quote types తో రాయవచ్చు?
Answer: Single quotes (' ') మరియు double quotes (" ") తో string ను రాయవచ్చు.
'Python' మరియు "Python" మధ్య తేడా ఏమిటి?
Answer: సాధారణంగా రెండూ ఒకే string value ను సూచిస్తాయి. తేడా ఉపయోగించిన quote type లో మాత్రమే ఉంటుంది.
String లో apostrophe ఉన్నప్పుడు ఏ quote type ఉపయోగించడం సులభం?
Answer: బయట double quotes ఉపయోగించడం సులభం. ఉదాహరణకు "It's easy".
Opening మరియు closing quotes వేర్వేరు రకాలుగా ఉంటే ఏమవుతుంది?
Answer: Python SyntaxError చూపించవచ్చు, ఎందుకంటే string సరిగా ముగియలేదని Python భావిస్తుంది.
Exam Questions
క్రింది code లో ఏ quote type ఉపయోగించారు? name = 'Ravi'
Answer:
Single quotes ఉపయోగించారు.
క్రింది code ను సరిచేయండి: message = 'It's good'.
Answer:
సరైన code: message = "It's good".
Python లో double quotes తో string తయారు చేసే ఒక ఉదాహరణ రాయండి.
Answer:
language = "Python"
Single quotes మరియు double quotes రెండూ string కోసం ఉపయోగించవచ్చా?
Answer:
అవును. రెండు quote types తోనూ string తయారు చేయవచ్చు.
Quick Revision
- Python లో strings కోసం single quotes లేదా double quotes ఉపయోగించవచ్చు.
- Single quotes: ‘Hello’
- Double quotes: “Hello”
- Opening మరియు closing quotes ఒకే రకంగా ఉండాలి.
- String లో apostrophe ఉంటే బయట double quotes ఉపయోగించవచ్చు.
- String లో double quote ఉంటే బయట single quotes ఉపయోగించవచ్చు.
- తరువాతి పాఠంలో strings లోని characters ను position ఆధారంగా ఎలా access చేయాలో నేర్చుకుంటాము.
