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` સાથે શું કરે છે?
- એ એક TypeError ફેંકે છે, કારણ કે તમે એક number ને એક string માં ઉમેરી શકતા નથી
- એ આપોઆપ "Balance: 500" print કરે છે
- એ number 505 આપે છે
- એ 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 હંમેશા એક પગલું ઓછું છે.