String Length
String Length
Python లో ఒక String లో ఎన్ని characters ఉన్నాయో తెలుసుకోవడానికి len() function ఉపయోగిస్తాము.
len() అంటే ఏమిటి?
len() అనేది ఒక built-in function. ఇది String లోని characters సంఖ్యను లెక్కించి integer value గా ఇస్తుంది.
ఎందుకు ఉపయోగిస్తాము?
String ఎంత పొడవుగా ఉందో తెలుసుకోవడానికి len() ఉపయోగిస్తాము. ఉదాహరణకు, password పొడవు సరిపోతుందా, username ఎన్ని characters కలిగి ఉంది, message length ఎంత ఉంది వంటి విషయాలు తెలుసుకోవచ్చు.
ఎలా పనిచేస్తుంది?
len(string) రూపంలో String ను len() లోపల ఇస్తాము. Python ఆ String లోని ప్రతి character ను లెక్కిస్తుంది. Letters, numbers, spaces మరియు special characters కూడా లెక్కలోకి వస్తాయి.
ముఖ్యమైన విషయం
String లోని space కూడా ఒక character గా లెక్కించబడుతుంది. ఉదాహరణకు, "Hi there" లో 8 characters ఉన్నాయి. ఇందులో ఒక space కూడా ఉంది.
Syntax
len(string_name)
Simple Explanation
ఒక String లో ఎన్ని characters ఉన్నాయో తెలుసుకోవాలంటే len() function ఉపయోగించాలి.
ఉదాహరణకు, "Python" లో 6 characters ఉన్నాయి. కాబట్టి len("Python") ఫలితం 6 అవుతుంది.
String లో spaces కూడా count అవుతాయి. len() ఫలితాన్ని ఒక variable లో store చేసి తర్వాత ఉపయోగించవచ్చు.
Real-Life Example
ఒక website లో password కనీసం 8 characters ఉండాలని నియమం ఉందనుకోండి. User ఇచ్చిన password length తెలుసుకోవడానికి Python లో len(password) ఉపయోగించవచ్చు. Length 8 కంటే తక్కువగా ఉంటే password చిన్నదని చెప్పవచ్చు.
Code Examples
String యొక్క length తెలుసుకోవడం
name = "Python"
length = len(name)
print(length)
ఈ code లో name variable లో ఒక String ను store చేశాము. తర్వాత len(name) ద్వారా String లోని characters సంఖ్యను తెలుసుకుని length variable లో ఉంచాము. చివరగా length ను print చేశాము.
6
Spaces కూడా count అవుతాయని చూడటం
message = "Hi there"
print(len(message))
ఈ code లో "Hi there" String లోని characters ను లెక్కిస్తున్నాము. ఇందులో Hi లో 2 characters, ఒక space, there లో 5 characters ఉన్నాయి. మొత్తం length 8 అవుతుంది.
8
Line-by-Line Explanation
ఈ line లో "Python" అనే String ను name variable లో store చేస్తాము. Python ఈ value ను name అనే పేరుతో గుర్తుంచుకుంటుంది.
ఈ line లో len(name) ద్వారా name String లోని characters సంఖ్యను లెక్కిస్తాము. Python 6 అనే ఫలితాన్ని ఇచ్చి, దాన్ని length variable లో store చేస్తుంది.
ఈ line లో length variable లో ఉన్న 6 విలువను screen పై చూపిస్తాము.
ఈ line లో Hi there అనే String ను message variable లో store చేస్తాము. ఇందులో letters తో పాటు ఒక space కూడా ఉంటుంది.
ఈ line లో message String యొక్క length ను నేరుగా లెక్కించి print చేస్తాము. Python letters మరియు మధ్యలో ఉన్న space ను కూడా count చేసి 8 ను చూపిస్తుంది.
Try It
Try this Python code:
text = "Python"
print(len(text))
Practice
Practice:
city అనే variable లో మీకు ఇష్టమైన city పేరును store చేసి, ఆ String length ను print చేయండి.
Hint:
print(len(city)) ఉపయోగించండి.
Practice:
sentence = "I like Python" అనే String లో మొత్తం ఎన్ని characters ఉన్నాయో print చేయండి. Space కూడా count అవుతుందని గుర్తుంచుకోండి.
Hint:
Sentence ను variable లో store చేసి, ఆ variable ను len() లో ఇవ్వండి.
Common Mistakes
len మాత్రమే రాసి parentheses పెట్టకపోవడం. ఉదాహరణ: len(text) బదులుగా len రాయడం.
len() ఒక function కాబట్టి parentheses తప్పనిసరిగా ఉపయోగించాలి. సరైన విధానం: len(text).
String లోని spaces count కావని అనుకోవడం.
Space కూడా ఒక character. కాబట్టి "Hi there" length 8 అని గుర్తుంచుకోండి.
Variable పేరు బదులుగా variable ను quotes లో రాయడం. ఉదాహరణ: len("text") అని రాయడం, కానీ text variable లోని value ను count చేయాలని అనుకోవడం.
Variable value ను count చేయాలంటే quotes లేకుండా variable పేరు వాడాలి: len(text).
length(text) అని రాయడం. Python లో String length కోసం సరైన function పేరు len().
ఎల్లప్పుడూ built-in len() function ను ఉపయోగించండి: len(text).
Interview Questions
Python లో String length తెలుసుకోవడానికి ఏ function ఉపయోగిస్తారు?
Answer:
len() function ఉపయోగిస్తారు. ఉదాహరణ: len("Python") ఫలితం 6.
len("Hello World") ఫలితం ఎంత?
Answer:
ఫలితం 11. ఎందుకంటే letters తో పాటు మధ్యలోని space కూడా count అవుతుంది.
len() function ఏ రకమైన value ను return చేస్తుంది?
Answer:
ఇది characters సంఖ్యను integer value గా return చేస్తుంది.
Exam Questions
క్రింది code output ఏమిటి? print(len("Code"))
Answer:
Output: 4
క్రింది code output ఏమిటి? word = "Hi there" మరియు print(len(word))
Answer:
Output: 8. Space కూడా ఒక character కాబట్టి మొత్తం 8.
ఖాళీ String యొక్క length ఎంత? len("")
Answer:
Output: 0. ఖాళీ String లో characters ఏవీ ఉండవు.
Quick Revision
- String length తెలుసుకోవడానికి
len()function ఉపయోగించాలి. len(string)String లోని characters సంఖ్యను return చేస్తుంది.- Letters, numbers, spaces మరియు special characters అన్నీ count అవుతాయి.
- Space కూడా ఒక character.
len("Python")ఫలితం 6.- తర్వాతి lesson లో String పై ఉపయోగించే వివిధ String Methods నేర్చుకుంటాము.
