str यूनिकोड कोड पॉइंटों का एक अपरिवर्तनीय सीक्वेंस है।
इसमें अक्षर, डायक्रिटिकल मार्क, पोज़िशनिंग अक्षर, संख्याएँ, करंसी सिंबल, इमोजी, विराम चिह्न, स्पेस और लाइन ब्रेक, और भी बहुत कुछ शामिल हो सकता है।
स्ट्रिंग सभी सामान्य सीक्वेंस ऑपरेशन लागू करती हैं और for item in <str> या for index, item in enumerate(<str>) सिंटैक्स की मदद से उन पर एक-एक करके चला जा सकता है।
हर अलग कोड पॉइंट (1 लंबाई की स्ट्रिंग) को बाईं ओर से 0-based index संख्या से, या दाईं ओर से -1-based index संख्या से रेफर किया जा सकता है।
स्ट्रिंग को <str> + <other str> या <str>.join(<iterable>) की मदद से जोड़ा जा सकता है, और <str>.split(<separator>) से टुकड़ों में बाँटा जा सकता है।
इनके अलावा स्ट्रिंग को फॉर्मैट करने और जोड़ने के कई और तरीके भी मौजूद हैं।
स्ट्रिंग के साथ और काम करने के लिए Python में खोजने, साफ करने, रूप बदलने, अनुवाद करने और ऐसे ही कई और कामों के लिए स्ट्रिंग मेथड का एक बड़ा सेट मौजूद है।
कुछ सबसे ज़्यादा इस्तेमाल होने वाले str मेथड ये हैं:
<str>.startswith(<substr>) और <str>.endswith(<substr>) से उपसर्ग/प्रत्यय की जाँच करना<str>.title(), <str>.upper()/<str>.lower(), और <str>.swapcase() जैसे मेथड से स्ट्रिंग का केस बदलना<str>.strip(<chars>), <str>.lstrip(<chars>), या <str>.rstrip(<chars>) की मदद से स्ट्रिंग के शुरू या अंत के अक्षर हटाना<str>.replace(<old>, <new>) मेथड से सबस्ट्रिंग बदलनाstr अपरिवर्तनीय होती है, इसलिए मेमोरी में उसकी वैल्यू बदल नहीं सकती। जो मेथड स्ट्रिंग को बदलती दिखती हैं, वे असल में उस str ऑब्जेक्ट की नई कॉपी या नया इंस्टेंस लौटाती हैं।
<str>.endswith(<suffix>) अगर स्ट्रिंग का अंत <suffix> से होता है तो True लौटाता है, वरना False।
>>> 'My heart breaks. 💔'.endswith('💔')
True
>>> 'cheerfulness'.endswith('ness')
True
# Punctuation is part of the string, so needs to be included in any endswith match.
>>> 'Do you want to 💃?'.endswith('💃')
False
>>> 'The quick brown fox jumped over the lazy dog.'.endswith('dog')
False
<str>.title() स्ट्रिंग को पढ़ती है और उसमें मिले हर "शब्द" के पहले "अक्षर" को बड़े अक्षर में बदल देती है।
Python में यह काफी हद तक इस पर निर्भर करता है कि कौन सा भाषा कोडेक इस्तेमाल हो रहा है और वह भाषा शब्दों और अक्षरों को कैसे दिखाती है।
किसी भाषा या अक्षर सेट के लिए लोकेल के नियम भी लागू हो सकते हैं।
>>> man_in_hat_th = 'ผู้ชายใส่หมวก'
>>> man_in_hat_ru = 'мужчина в шляпе'
>>> man_in_hat_ko = '모자를 쓴 남자'
>>> man_in_hat_en = 'the man in the hat.'
>>> man_in_hat_th.title()
'ผู้ชายใส่หมวก'
>>> man_in_hat_ru.title()
'Мужчина В Шляпе'
>>> man_in_hat_ko.title()
'모자를 쓴 남자'
>> man_in_hat_en.title()
'The Man In The Hat.'
<str>.strip(<chars>) str की एक कॉपी लौटाता है जिसमें से शुरू और अंत के <chars> हटा दिए गए हों।
<chars> में दिए गए कोड पॉइंट उपसर्ग या प्रत्यय नहीं होते। स्ट्रिंग के दोनों सिरों से शुरू करके उन कोड पॉइंटों के सभी संयोजन हटा दिए जाते हैं।
अगर <chars> के लिए कुछ नहीं दिया जाए, तो व्हाइटस्पेस कोड पॉइंटों के सभी संयोजन हटा दिए जाते हैं।
अगर सिर्फ बाईं ओर से या दाईं ओर से हटाना हो, तो <str>.lstrip(<chars>) और <str>.rstrip(<chars>) इस्तेमाल किए जा सकते हैं।
# This will remove "https://", because it can be formed from "/stph:".
>>> 'https://unicode.org/emoji/'.strip('/stph:')
'unicode.org/emoji'
# Removal of all whitespace from both ends of the str.
>>> ' 🐪🐪🐪🌟🐪🐪🐪 '.strip()
'🐪🐪🐪🌟🐪🐪🐪'
>>> justification = 'оправдание'
>>> justification.strip('еина')
'оправд'
# Prefix and suffix in one step.
>>> 'unaddressed'.strip('dnue')
'address'
>>> ' unaddressed '.strip('dnue ')
'address'
<str>.replace(<substring>, <replacement substring>) स्ट्रिंग की एक कॉपी लौटाता है जिसमें <substring> के सभी मौजूद रूपों को <replacement substring> से बदल दिया गया हो।
नीचे इस्तेमाल किया गया उद्धरण The Hunting of the Snark से लिया गया है, जिसे Lewis Carroll ने लिखा।
# The Hunting of the Snark, by Lewis Carroll
>>> quote = '''
"Just the place for a Snark!" the Bellman cried,
As he landed his crew with care;
Supporting each man on the top of the tide
By a finger entwined in his hair.
"Just the place for a Snark! I have said it twice:
That alone should encourage the crew.
Just the place for a Snark! I have said it thrice:
What I tell you three times is true."
'''
>>> quote.replace('Snark', '🐲')
...
'\n"Just the place for a 🐲!" the Bellman cried,\n As he landed his crew with care;\nSupporting each man on the top of the tide\n By a finger entwined in his hair.\n\n"Just the place for a 🐲! I have said it twice:\n That alone should encourage the crew.\nJust the place for a 🐲! I have said it thrice:\n What I tell you three times is true."\n'
>>> 'bookkeeper'.replace('kk', 'k k')
'book keeper'
🌟Python 3.9 में नया जोड़ा गया
Python 3.9 में दो नए स्ट्रिंग मेथड जुड़े हैं जो उपसर्ग और प्रत्यय हटाना बहुत आसान बना देते हैं।
<str>.removeprefix(<substring>) स्ट्रिंग को उपसर्ग के बिना लौटाता है (string[len(<substring>):])। अगर <substring> मौजूद न हो, तो मूल स्ट्रिंग की एक कॉपी लौटा दी जाती है।
# removing a prefix
>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'bookkeeper'.removeprefix('book')
'keeper'
<str>.removesuffix(<substring>) स्ट्रिंग को प्रत्यय के बिना लौटाता है (string[:-len(substring)])। अगर <substring> मौजूद न हो, तो मूल स्ट्रिंग की एक कॉपी लौटा दी जाती है।
# removing a suffix
>>> 'TestHook'.removesuffix('Hook')
'Test'
>>> 'bookkeeper'.removesuffix('keeper')
'book'
और उदाहरणों और मेथड के लिए अनौपचारिक ट्यूटोरियल एक अच्छी शुरुआत है। Python के डॉक्युमेंटेशन में How to Unicode यूनिकोड, एन्कोडिंग, बाइट और Python में स्ट्रिंग के साथ काम करने की दूसरी तकनीकी बातों पर बहुत विस्तार से बताता है।
Python re मॉड्यूल के ज़रिए रेगुलर एक्सप्रेशन भी सपोर्ट करता है, जिसे आगे किसी कॉन्सेप्ट में समझाया जाएगा।