Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
बुनियादी स्ट्रिंग ऑपरेशन्स
Strings वे टेक्स्ट के टुकड़े होते हैं। इन्हें उद्धरण चिह्नों के बीच किसी भी चीज़ के रूप में परिभाषित किया जा सकता है:
astring = "Hello world!"
astring2 = 'Hello world!'
जैसा कि आप देख सकते हैं, आपने सबसे पहले एक साधारण वाक्य को प्रिंट करना सीखा। इस वाक्य को Python द्वारा एक स्ट्रिंग के रूप में संग्रहीत किया गया था। हालाँकि, स्ट्रिंग्स को सीधे प्रिंट करने के बजाय, हम उन पर करने योग्य विभिन्न क्रियाओं की खोज करेंगे।
आप स्ट्रिंग को असाइन करने के लिए सिंगल कोट्स का भी उपयोग कर सकते हैं। हालाँकि, आपको समस्या का सामना करना पड़ेगा यदि असाइन करने के लिए स्वयं मूल्य में सिंगल कोट्स हैं। उदाहरण के लिए, इस ब्रैकेट में स्ट्रिंग को असाइन करने के लिए (सिंगल कोट्स ' ' हैं) आपको केवल डबल कोट्स का उपयोग करना होगा, जैसे
astring = "Hello world!"
print("single quotes are ' '")
print(len(astring))
यह 12 प्रिंट करता है, क्योंकि "Hello world!" में 12 वर्ण होते हैं, जिसमें विराम चिह्न और स्पेस शामिल हैं।
astring = "Hello world!"
print(astring.index("o"))
यह 4 प्रिंट करता है, क्योंकि "o" अक्षर की पहली उपस्थिति का स्थान पहले वर्ण से 4 वर्ण दूर है। ध्यान दें कि वाक्यांश में वास्तव में दो o's हैं - यह विधि केवल पहले को पहचानती है।
लेकिन इसने 5 क्यों नहीं प्रिंट किया? क्या "o" स्ट्रिंग में पांचवा वर्ण नहीं है? चीजों को अधिक सरल बनाने के लिए, Python (और अधिकांश अन्य प्रोग्रामिंग भाषाएं) 1 के बजाय 0 से शुरू होती हैं। इसलिए "o" का इंडेक्स 4 है।
astring = "Hello world!"
print(astring.count("l"))
उन लोगों के लिए जो बेवकूफ फोंट का उपयोग कर रहे हैं, यह एक लोअरकेस L है, न कि नंबर एक। यह स्ट्रिंग में l's की संख्या को गिनता है। इसलिए, इसे 3 प्रिंट करना चाहिए।
astring = "Hello world!"
print(astring[3:7])
यह स्ट्रिंग की एक स्लाइस प्रिंट करता है, जो 3 इंडेक्स से शुरू होकर 6 इंडेक्स पर समाप्त होती है। लेकिन 7 क्यों नहीं? फिर से, अधिकांश प्रोग्रामिंग भाषाएं ऐसा करती हैं - इससे ब्रैकेट के अंदर गणित करना आसान हो जाता है।
यदि आप केवल ब्रैकेट में एक नंबर छोड़ते हैं, तो यह आपको उस इंडेक्स पर अकेले वर्ण देगा। यदि आप पहला नंबर छोड़ देते हैं लेकिन कोलन रखते हैं, तो यह आपको आरंभ से उस संख्या तक एक स्लाइस देगा जिसे आपने छोड़ दिया। यदि आप दूसरा नंबर छोड़ देते हैं, तो यह आपको पहले नंबर से अंत तक का एक स्लाइस देगा।
आप यहां तक कि ब्रैकेट के अंदर नकारात्मक संख्याएं डाल सकते हैं। वे स्ट्रिंग की शुरुआत के बजाय अंत से शुरू करने का एक आसान तरीका हैं। इस तरह, -3 का मतलब "अंत से तीसरा वर्ण" होता है।
astring = "Hello world!"
print(astring[3:7:2])
यह 3 से 7 तक के स्ट्रिंग के वर्ण प्रिंट करता है, जिसमें एक वर्ण को छोड़ दिया जाता है। यह एक्सटेंडेड स्लाइस सिंटैक्स है। सामान्य रूप [start:stop:step] है।
astring = "Hello world!"
print(astring[3:7])
print(astring[3:7:1])
ध्यान दें कि दोनों एक ही आउटपुट उत्पन्न करते हैं
C में strrev जैसी कोई फंक्शन नहीं है जो स्ट्रिंग को रिवर्स कर सके। लेकिन ऊपर बताए गए प्रकार के स्लाइस सिंटैक्स के साथ आप आसानी से स्ट्रिंग को रिवर्स कर सकते हैं जैसे
astring = "Hello world!"
print(astring[::-1])
यह
astring = "Hello world!"
print(astring.upper())
print(astring.lower())
ये नए स्ट्रिंग बनाते हैं जिसमें सभी अक्षर क्रमशः अपरकेस और लोअरकेस में परिवर्तित होते हैं।
astring = "Hello world!"
print(astring.startswith("Hello"))
print(astring.endswith("asdfasdfasdf"))
यह यह निर्धारित करने के लिए उपयोग किया जाता है कि स्ट्रिंग कुछ से शुरू होती है या कुछ से समाप्त होती है। पहला True प्रिंट करेगा, क्योंकि स्ट्रिंग "Hello" से शुरू होती है। दूसरा False प्रिंट करेगा, क्योंकि स्ट्रिंग निश्चित रूप से "asdfasdfasdf" से समाप्त नहीं होती है।
astring = "Hello world!"
afewwords = astring.split(" ")
यह स्ट्रिंग को सूची में एक साथ समूहित स्ट्रिंग्स के झुंड में विभाजित करता है। क्योंकि यह उदाहरण स्पेस पर विभाजित होता है, सूची का पहला आइटम "Hello" होगा, और दूसरा "world!" होगा।
Exercise
कोड को सही जानकारी प्रिंट करने के लिए स्ट्रिंग बदलकर सुधारने का प्रयास करें।
s = "Hey there! what should this string be?"
# Length should be 20
print("Length of s = %d" % len(s))
# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))
# Number of a's should be 2
print("a occurs %d times" % s.count("a"))
# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end
# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())
# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())
# Check how a string starts
if s.startswith("Str"):
print("String starts with 'Str'. Good!")
# Check how a string ends
if s.endswith("ome!"):
print("String ends with 'ome!'. Good!")
# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))
s = "Strings are awesome!"
# Length should be 20
print("Length of s = %d" % len(s))
# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))
# Number of a's should be 2
print("a occurs %d times" % s.count("a"))
# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end
# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())
# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())
# Check how a string starts
if s.startswith("Str"):
print("String starts with 'Str'. Good!")
# Check how a string ends
if s.endswith("ome!"):
print("String ends with 'ome!'. Good!")
# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))
test_object("s", incorrect_msg="Make sure you change the string assigned to `s` to match the exercise instructions.")
success_msg("Great work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!