String operations: slicing, negative indexing, length, concatenation

Slicing, negative indexing, len, और concatenation वे चार चालें हैं जो आपको एक string को टुकड़ों में काटने, इसे count करने, और इसे वापस जोड़ने देती हैं, जो ठीक वैसे ही है जैसे PocketMoney एक raw entry पढ़ता है।

9 min read · 12 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

एक line, तीन जवाब

आपके PocketMoney tracker को अभी एक raw entry एक string के रूप में मिली:

2026-01-15 samosa 30

उस अकेली line से program को तीन अलग जवाब निकालने होंगे: year (2026), item (samosa), और amount (30)। किसी ने उन्हें साफ़ boxes में type नहीं किया; यह सब text का एक blob है।

एक गंदी string से साफ़ टुकड़े निकालना असली software में एक रोज़ का काम है, और Python आपको इसे करने के लिए चार छोटे tools देता है। यह page वे चार tools हैं।

Theory

एक string numbered lockers की एक पंक्ति है

text samosa को lockers की एक पंक्ति के रूप में सोचिए, प्रति locker एक character, बाईं ओर से 0 से numbered: s locker 0 है, a 1 है, m 2 है, और आगे। Python किसी भी locker की सामग्री उसके number से आपको दे सकता है, या एक बार में lockers का एक पूरा हिस्सा। एक बार आप एक string को numbered lockers के रूप में देखते हैं, indexing और slicing रहस्यमय होना बंद कर देते हैं: आप बस locker numbers नाम दे रहे हैं।

Theory

Indexing और length

एक character को square brackets और एक position के साथ access कीजिए, 0 से गिनते हुए:

  • word[0] पहला character है।
  • Negative indexing दाईं ओर से गिनती है: word[-1] आख़िरी character है, word[-2] दूसरा आख़िरी। तब सुविधाजनक जब आप length नहीं जानते।

len(word) देता है कि string में कितने characters हैं। एक उपयोगी जोड़ी: आख़िरी character हमेशा word[len(word) - 1] है, जो ठीक वही है जो word[-1] आपको छोटे तरीक़े से देता है।

Practical

item name को indexing करना

item = "samosa"

print(item[0])        # s  (first locker)
print(item[-1])       # a  (last locker, counting from right)
print(len(item))      # 6  (six characters)
print(item[len(item) - 1])   # a  (same as item[-1], the long way)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

Slicing: lockers का एक हिस्सा

Slicing word[start:stop] के साथ characters का एक run लेता है। सुनहरा नियम, और नंबर एक exam trap: stop index बाहर रखा जाता है। word[0:4] 0, 1, 2, 3 पर characters देता है, पर 4 नहीं।

तो हमारी entry 2026-01-15 samosa 30 के लिए:

  • positions 0 से 3 2026 रखते हैं, तो record[0:4] year देता है।
  • start छोड़ना मतलब 'शुरुआत से', stop छोड़ना मतलब 'अंत तक': record[0:4] और record[:4] एक जैसे हैं।

Think first

slice की भविष्यवाणी कीजिए

word = "samosa" के लिए, tap करने से पहले अपने दिमाग़ में word[1:4] हल कीजिए। याद रखिए: start शामिल है, stop बाहर है।

Show the answer

amo। index 1 (a) पर शुरू, index 2 (m) और index 3 (o) शामिल, फिर index 4 (s) से पहले STOP। तो indices 1, 2, 3 'amo' देते हैं। सबसे आम ग़लत जवाब 'amos' है, जो तब होता है जब आप भूल जाते हैं कि stop index (4) शामिल नहीं है।

Practical

PocketMoney entry को Parse करना

record = "2026-01-15 samosa 30"

year = record[0:4]        # "2026"  (positions 0,1,2,3)
last_char = record[-1]    # "0"     (rightmost character)
total_len = len(record)   # 20      (counts spaces too)

print("Year:", year)
print("Length:", total_len)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

Concatenation: + से जोड़ना

+ operator strings को अंत से अंत तक जोड़ता है:

"Rs. " + "30" "Rs. 30" देता है।

एक सख़्त नियम जो Python नहीं झुकाएगा: + के दोनों तरफ़ strings होने चाहिए। अगर एक तरफ़ एक number है, आपको एक TypeError मिलता है। fix str() है, जो एक number को उसके text रूप में बदलता है:

"Total: " + str(30) "Total: 30" देता है।

यह लगभग हर beginner को कम से कम एक बार पकड़ता है।

Quiz

Python expression `"Balance: " + 500` के साथ क्या करता है?

  1. यह एक TypeError फेंकता है, क्योंकि आप एक number को एक string में नहीं जोड़ सकते
  2. यह अपने-आप "Balance: 500" print करता है
  3. यह number 505 देता है
  4. यह number को अनदेखा करता है और "Balance: " print करता है
Show the answer

यह एक TypeError फेंकता है, क्योंकि आप एक number को एक string में नहीं जोड़ सकते

Python concatenation के दौरान चुपचाप types convert नहीं करता: बाईं ओर एक string और दाईं ओर एक integer एक TypeError है। कुछ भाषाओं के उलट, यह आपके इरादे का अनुमान नहीं लगाएगा। fix number को लपेटना है: "Balance: " + str(500)। Option B किसी भी automatic conversion की उम्मीद करने वाले के लिए जाल है।

Watch out

Strings जगह पर नहीं बदलतीं

यहाँ हर operation string को पढ़ता है या एक नई बनाता है; उनमें से कोई भी original को edit नहीं करता। record[0:4] आपको एक ताज़ा टुकड़ा देता है और record को अछूता छोड़ता है, और आप record[0] = "X" नहीं कर सकते (वह एक TypeError है)। Python strings immutable हैं। एक string को 'बदलने' के लिए आप एक नई बनाते हैं, उदाहरण के लिए new = "X" + record[1:]। Exams इसे test करना पसंद करते हैं।

Theory

आप इससे पहले मिले हैं, C में

BCA104 में एक string एक char array था जो \0 में ख़त्म होता था, और आप इसकी length \0 तक loop करके पाते थे, character-दर-character। Python \0 छुपाता है और आपको len(), slicing, और negative indexing मुफ़्त में देता है। एक ही विचार (text characters का एक sequence है), बहुत कम मैनुअल काम। आगे आप string METHODS (split, replace, upper) से मिलेंगे, जो इन raw चालों को one-word tools में बदलते हैं।

Summary

Key takeaways

  • Indexing word[i] इस्तेमाल करता है, 0 से शुरू; negative indexing दाईं ओर से गिनती है, तो word[-1] आख़िरी character है।
  • Slicing word[start:stop] start शामिल करता है पर stop बाहर रखता है; 'शुरुआत से' या 'अंत तक' के लिए किसी भी छोर को छोड़ें।
  • len(word) character count देता है, spaces समेत।
  • + से concatenation के दोनों तरफ़ strings चाहिए; numbers को str() में लपेटें या एक TypeError पाएँ।
  • Strings immutable हैं: operations नई strings देते हैं, वे कभी original edit नहीं करते।
  • Memory hook: [ : ] से काटें, len से count करें, + से जोड़ें, और stop हमेशा एक क़दम कम है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Conditions, Loops and Strings

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

String operations: slicing, negative indexing, length, concatenation · Programming Skills · Gri-Learn