Python में str यूनिकोड कोड पॉइंट्स का एक अपरिवर्तनीय अनुक्रम है।
इनमें अक्षर, मात्रा-चिह्न, स्थिति-निर्धारक चिह्न, संख्याएँ, मुद्रा-प्रतीक, इमोजी, विराम-चिह्न, स्पेस और लाइन ब्रेक वाले अक्षर, और भी बहुत कुछ शामिल हो सकता है।
किसी स्ट्रिंग में किस तरह की जानकारी एनकोड होती है (या, "कंप्यूटर को यह कैसे पता चलता है कि शून्य और एक को अक्षरों में कैसे बदलना है?"), इस पर गहराई से जानने के लिए यह ब्लॉग पोस्ट आज भी बहुत उपयोगी है।
Python के डॉक्युमेंटेशन में एक बहुत विस्तृत unicode HOWTO भी है, जिसमें str, bytes और re मॉड्यूल में यूनिकोड स्पेसिफिकेशन के लिए Python के समर्थन, लोकेल से जुड़ी बातों, और एनकोडिंग तथा ट्रांसलेशन से जुड़ी कुछ आम समस्याओं पर चर्चा की गई है।
स्ट्रिंग्स सभी सामान्य अनुक्रम संक्रियाएँ को सपोर्ट करती हैं और इन पर 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>) से अलग-अलग हिस्सों में बाँटा जा सकता है।
इनमें फ़ॉर्मैटिंग, जोड़ने और टेम्पलेट बनाने के कई और विकल्प भी मिलते हैं।
किसी str लिटरल को सिंगल ' या डबल " कोट्स से घोषित किया जा सकता है। ज़रूरत पड़ने पर एस्केप के लिए \ अक्षर मौजूद है।
>>> single_quoted = 'These allow "double quoting" without "escape" characters.'
>>> double_quoted = "These allow embedded 'single quoting', so you don't have to use an 'escape' character."
मल्टी-लाइन स्ट्रिंग्स ''' या """ से घोषित की जाती हैं।
>>> triple_quoted = '''Three single quotes or "double quotes" in a row allow for multi-line string literals.
Line break characters, tabs and other whitespace is fully supported. Remember - The escape "\" character is also available if needed (as can be seen below).
You\'ll most often encounter multi-line strings as "doc strings" or "doc tests" written just below the first line of a function or class definition.
They\'re often used with auto documentation ✍ tools.
'''
str(<object>) कंस्ट्रक्टर का इस्तेमाल दूसरे ऑब्जेक्ट्स से स्ट्रिंग बनाने या उन्हें स्ट्रिंग में बदलने के लिए किया जा सकता है:
>>> my_number = 42
>>> str(my_number)
...
"42"
str(<object>) कंस्ट्रक्टर से ऑब्जेक्ट को स्ट्रिंग में बदला तो जा सकता है, लेकिन यह किसी ऑब्जेक्ट के एलिमेंट्स पर एक-एक करके नहीं चलता और न ही ऑब्जेक्ट को खोलकर उसके हिस्से अलग करता है।
यह list(), set(), dict() या tuple() जैसे दूसरे डेटा टाइप के कंस्ट्रक्टर के व्यवहार से अलग है, और इसके नतीजे चौंकाने वाले हो सकते हैं।
>>> numbers = [1,3,5,7]
>>> str(numbers)
...
'[1,3,5,7]'
किसी str के कोड पॉइंट्स तक बाईं ओर से 0-based index संख्या से पहुँचा जा सकता है:
creative = '창의적인'
>>> creative[0]
'창'
>>> creative[2]
'적'
>>> creative[3]
'인'
इंडेक्सिंग दाईं ओर से भी काम करती है, जो -1-based index से शुरू होती है:
creative = '창의적인'
>>> creative[-4]
'창'
>>> creative[-2]
'적'
>>> creative[-1]
'인'
Python में “अक्षर” या "रून" जैसा कोई अलग टाइप नहीं है, इसलिए किसी स्ट्रिंग को इंडेक्स करने पर लंबाई 1 की नई str बनती है:
>>> website = "exercism"
>>> type(website[0])
<class 'str'>
>>> len(website[0])
1
>>> website[0] == website[0:1] == 'e'
True
सबस्ट्रिंग्स को स्लाइस नोटेशन से चुना जा सकता है, जिसमें <str>[<start>:<stop>:<step>] का इस्तेमाल करके नई स्ट्रिंग बनाई जाती है।
नतीजों में stop इंडेक्स शामिल नहीं होता।
अगर start नहीं दिया गया है, तो शुरुआती इंडेक्स 0 होगा।
अगर stop नहीं दिया गया है, तो stop इंडेक्स स्ट्रिंग के अंत तक होगा।
moon_and_stars = '🌟🌟🌙🌟🌟⭐'
>>> moon_and_stars[1:4]
'🌟🌙🌟'
>>> moon_and_stars[:3]
'🌟🌟🌙'
>>> moon_and_stars[3:]
'🌟🌟⭐'
>>> moon_and_stars[:-1]
'🌟🌟🌙🌟🌟'
>>> moon_and_stars[:-3]
'🌟🌟🌙'
स्ट्रिंग्स को <str>.split(<separator>) की मदद से छोटी-छोटी स्ट्रिंग्स में भी तोड़ा जा सकता है, जो सबस्ट्रिंग्स की एक list लौटाता है।
बिना कोई आर्गुमेंट दिए <str>.split() का इस्तेमाल करने पर स्ट्रिंग स्पेस वाली जगहों पर टूट जाती है।
>>> cat_ipsum = "Destroy house in 5 seconds command the hooman."
>>> cat_ipsum.split()
...
['Destroy', 'house', 'in', '5', 'seconds', 'command', 'the', 'hooman.']
>>> cat_words = "feline, four-footed, ferocious, furry"
>>> cat_words.split(',')
...
['feline', ' four-footed', ' ferocious', ' furry']
>>> colors = """red,
orange,
green,
purple,
yellow"""
>>> colors.split(',\n')
['red', 'orange', 'green', 'purple', 'yellow']
स्ट्रिंग्स को + ऑपरेटर से जोड़ा जा सकता है।
इस तरीके का इस्तेमाल कम ही करना चाहिए, क्योंकि इसका प्रदर्शन बहुत अच्छा नहीं होता और इसे बनाए रखना भी आसान नहीं है।
language = "Ukrainian"
number = "nine"
word = "дев'ять"
sentence = word + " " + "means" + " " + number + " in " + language + "."
>>> print(sentence)
...
"дев'ять means nine in Ukrainian."
अगर अलग-अलग स्ट्रिंग्स के किसी list, tuple, set या किसी और कलेक्शन को एक ही str में जोड़ना हो, तो <str>.join(<iterable>) बेहतर विकल्प है:
# str.join() makes a new string from the iterables elements.
>>> chickens = ["hen", "egg", "rooster"] # Lists are iterable.
>>> ' '.join(chickens)
'hen egg rooster'
# Any string can be used as the joining element.
>>> ' :: '.join(chickens)
'hen :: egg :: rooster'
>>> ' 🌿 '.join(chickens)
'hen 🌿 egg 🌿 rooster'
# Any iterable can be used as input.
>>> flowers = ("rose", "daisy", "carnation") # Tuples are iterable.
>>> '*-*'.join(flowers)
'rose*-*daisy*-*carnation'
>>> flowers = {"rose", "daisy", "carnation"} # Sets are iterable, but output order is not guaranteed.
>>> '*-*'.join(flowers)
'rose*-*carnation*-*daisy'
>>> phrase = "This is my string" # Strings are iterable, but be careful!
>>> '..'.join(phrase)
'T..h..i..s.. ..i..s.. ..m..y.. ..s..t..r..i..n..g'
# Separators are inserted **between** elements, but can be any string (including spaces).
# This can be exploited for interesting effects.
>>> under_words = ['under', 'current', 'sea', 'pin', 'dog', 'lay']
>>> separator = ' ⤴️ under' # Note the leading space, but no trailing space.
>>> separator.join(under_words)
'under ⤴️ undercurrent ⤴️ undersea ⤴️ underpin ⤴️ underdog ⤴️ underlay'
# The separator can be composed different ways, as long as the result is a string.
>>> upper_words = ['upper', 'crust', 'case', 'classmen', 'most', 'cut']
>>> separator = ' 🌟 ' + upper_words[0] # This becomes one string, similar to ' ⤴️ under'.
>>> separator.join(upper_words)
'upper 🌟 uppercrust 🌟 uppercase 🌟 upperclassmen 🌟 uppermost 🌟 uppercut'
स्ट्रिंग्स सभी सामान्य अनुक्रम संक्रियाएँ को सपोर्ट करती हैं।
अलग-अलग कोड पॉइंट्स पर for item in <str> की मदद से लूप चलाया जा सकता है।
इंडेक्स और उनके एलिमेंट्स, दोनों को, for index, item in enumerate(<str>) की मदद से लूप में एक-एक करके पढ़ा जा सकता है।
>>> exercise = 'လေ့ကျင့်'
# Note that there are more code points than perceived glyphs or characters.
# Care should be used when iterating over languages that use
# combining characters, or when dealing with emoji.
>>> for code_point in exercise:
... print(code_point)
...
လ
ေ
့
က
ျ
င
်
့
# Using enumerate will give both the value and index position of each element.
>>> for index, code_point in enumerate(exercise):
... print(index, ": ", code_point)
...
0 : လ
1 : ေ
2 : ့
3 : က
4 : ျ
5 : င
6 : ်
7 : ့
Python में स्ट्रिंग मेथड का एक बड़ा सेट मौजूद है, जो खोजने, साफ़ करने, तोड़ने, बदलने, अनुवाद करने और कई दूसरे कामों में मदद करता है। इनमें से कुछ मेथड पर एक और अभ्यास में चर्चा की गई है।
Python में स्ट्रिंग्स को फ़ॉर्मैटिंग करने और टेम्पलेटिंग करने के लिए बहुत-से टूल भी हैं, और re (रेगुलर एक्सप्रेशन), difflib (अनुक्रम की तुलना) तथा textwrap मॉड्यूल के ज़रिए और भी बेहतर तरीके से टेक्स्ट प्रोसेस किया जा सकता है। Python में स्ट्रिंग फ़ॉर्मैटिंग की बढ़िया शुरुआत के लिए Real Python की यह पोस्ट देखिए। स्ट्रिंग मेथड की शुरुआत के लिए उसी साइट पर Strings and Character Data in Python देखिए।
str (एक टेक्स्ट अनुक्रम) के अलावा Python में इससे जुड़े बाइनरी अनुक्रम टाइप भी हैं, जिनका ज़िक्र binary data services में मिलता है -- bytes (एक बाइनरी अनुक्रम), bytearray, और बाइनरी डेटा को कुशलता से संग्रहीत करने तथा संभालने के लिए memoryview।
इसके अलावा, Streams की मदद से नेटवर्क कनेक्शन पर बिना कॉलबैक इस्तेमाल किए बाइनरी डेटा भेजा और प्राप्त किया जा सकता है।