Escape Characters
Escape Characters అంటే ఏమిటి?
Python stringలో కొన్ని ప్రత్యేక charactersను నేరుగా రాయడం కష్టం లేదా వాటికి ప్రత్యేక అర్థం ఉంటుంది. అలాంటి charactersను stringలో ఉపయోగించడానికి Escape Characters ఉపయోగిస్తాము.
Escape character సాధారణంగా backslash ()తో ప్రారంభమవుతుంది. Backslash తరువాత వచ్చే characterకు Python ఒక ప్రత్యేక అర్థాన్ని ఇస్తుంది.
ఎందుకు ఉపయోగిస్తాము?
- Stringలో కొత్త line ఇవ్వడానికి
- Textలో tab space ఇవ్వడానికి
- Stringలో double quote లేదా single quote చూపించడానికి
- File pathలో backslash చూపించడానికి
సాధారణ Escape Sequences
n– కొత్త linet– tab space"– double quote'– single quote\– ఒక backslash
Python stringను చదివేటప్పుడు backslashతో ఉన్న ఈ combinationsను ప్రత్యేక charactersగా మార్చి output చూపిస్తుంది.
Simple Explanation
ఒక messageను రెండు linesలో చూపించాలంటే stringలో n వాడాలి. రెండు పదాల మధ్య ఎక్కువ space కావాలంటే t వాడాలి. Stringలో quote characterనే చూపించాలంటే దాని ముందు backslash పెట్టాలి.
ఉదాహరణకు, "He said, "Hello"" అనే string outputలో He said, "Hello"గా కనిపిస్తుంది. ఇక్కడ inner double quotes stringను ముగించకుండా ఉండటానికి " ఉపయోగించాం.
Escape sequenceలోని backslash సాధారణంగా outputలో కనిపించదు. అది తరువాతి characterకు ప్రత్యేక అర్థాన్ని ఇస్తుంది.
Real-Life Example
ఒక greeting cardలో పేరు, message, address వేర్వేరు linesలో ఉండవచ్చు. Computerలో ఆ linesను ఒకే stringలో రాయడానికి n ఉపయోగిస్తాము. అలాగే addressలో folder path చూపించేటప్పుడు backslashను చూపించడానికి \ ఉపయోగించవచ్చు.
Code Examples
కొత్త line మరియు tab ఉపయోగించడం
print("HellonPython")
print("Name:tRavi")
మొదటి stringలో n ఉండటం వల్ల Hello మరియు Python రెండు వేర్వేరు linesలో కనిపిస్తాయి. రెండవ stringలో t ఉండటం వల్ల Name: తరువాత tab space వస్తుంది.
Hello Python Name: Ravi
Stringలో double quote చూపించడం
print("He said, "Python is easy."")
String double quotesతో ప్రారంభమైంది మరియు ముగిసింది. మధ్యలోని double quotes stringను ముగించకుండా ఉండటానికి "గా రాశాము.
He said, "Python is easy."
Stringలో backslash చూపించడం
print("C:\Users\Student")
ఒక backslashను outputలో చూపించడానికి codeలో రెండు backslashes, అంటే \, రాయాలి. అందువల్ల Windows-style path సరైన విధంగా కనిపిస్తుంది.
C:UsersStudent
Line-by-Line Explanation
ఈ line stringను print చేస్తుంది. nను Python కొత్త lineగా అర్థం చేసుకుంటుంది. అందువల్ల Hello ఒక lineలో, Python తరువాతి lineలో కనిపిస్తాయి.
ఈ line Name: మరియు Raviను print చేస్తుంది. t tab spaceను సూచిస్తుంది కాబట్టి Ravi కొంత ఖాళీ తరువాత కనిపిస్తుంది.
ఈ line ఒక sentenceను print చేస్తుంది. " వల్ల sentenceలో double quote characters కనిపిస్తాయి, కానీ అవి Python stringను ముందుగానే ముగించవు.
ఈ line file pathను print చేస్తుంది. ప్రతి \ ఒకే backslashగా outputలో కనిపిస్తుంది. అందువల్ల C:UsersStudent అనే path వస్తుంది.
Try It
Try this Python code:
print("Name:tRavi")
print("Message:nKeep learning Python!")
Practice
Practice: ఒకే Python stringలో మీ పేరు మొదటి lineలో, మీ city రెండవ lineలో కనిపించేలా code రాయండి.
Hint: nను name మరియు city మధ్య ఉపయోగించండి.
Practice: Outputలో He said, "Learn Python" అనే text కనిపించేలా print statement రాయండి.
Hint: String double quotesతో ఉంటే, లోపల కనిపించే double quotes కోసం " ఉపయోగించండి.
Practice: Outputలో C:ProjectsPython కనిపించేలా print statement రాయండి.
Hint: ప్రతి output backslash కోసం codeలో \ రాయాలి.
Common Mistakes
print("Helloను codeలో నిజంగా రెండు linesగా విభజించి stringను సరిగ్గా close చేయకపోవడం.
Python")
ఒకే lineలో n escape sequenceను రాయండి: print("HellonPython").
File pathలో ఒక backslash మాత్రమే రాయడం, ఉదాహరణకు print("C:Users").
Backslashను outputలో చూపించడానికి ప్రతి backslashను రెండుసార్లు రాయండి: print("C:\Users").
String double quotesతో ఉన్నప్పుడు లోపల double quoteను escape చేయకుండా రాయడం.
లోపల double quote ముందు పెట్టండి. ఉదాహరణ: print("He said, "Hi"").
nను letters nతో కలిపి గందరగోళం చేయడం. n కొత్త line ఇస్తుంది; n మాత్రం సాధారణ letter.
కొత్త line కోసం backslash మరియు n కలిపిన nనే ఉపయోగించండి.
Interview Questions
Pythonలో Escape Character అంటే ఏమిటి?
Answer: Backslashతో ప్రారంభమై stringలో ప్రత్యేక అర్థాన్ని ఇచ్చే character combinationను Escape Character లేదా Escape Sequence అంటారు.
n మరియు t దేనికి ఉపయోగిస్తారు?
Answer: n కొత్త line కోసం, t tab space కోసం ఉపయోగిస్తారు.
Python stringలో double quoteను ఎలా చూపిస్తారు?
Answer: Double quote ముందు backslash పెట్టాలి. ఉదాహరణకు ".
Stringలో ఒక backslashను outputగా చూపించడానికి codeలో ఏమి రాయాలి?
Answer: రెండు backslashes, అంటే \, రాయాలి.
Exam Questions
క్రింది code output ఏమిటి? print("AnB")
Answer:
Output రెండు linesలో వస్తుంది: మొదటి lineలో A, రెండవ lineలో B.
కొత్త line కోసం ఉపయోగించే Escape Sequence ఏది?
Answer:
n.
Tab space కోసం ఉపయోగించే Escape Sequence ఏది?
Answer:
t.
క్రింది codeలో output ఏమిటి? print("He said, "Hi"")
Answer:
He said, "Hi".
Quick Revision
తో ప్రారంభమయ్యే special combinationsను Escape Sequences అంటారు.nకొత్త lineను సృష్టిస్తుంది.ttab spaceను సృష్టిస్తుంది."stringలో double quoteను చూపిస్తుంది.'stringలో single quoteను చూపిస్తుంది.\outputలో ఒక backslashను చూపిస్తుంది.- Escape Characters stringsలో formatting మరియు special symbols చూపించడానికి ఉపయోగపడతాయి.
