Indexing in Python

Indexing in Python

In Python, specific character in string data can be accessed directly using a numeric index or key value. This process is referred to as indexing.

पायथन में, स्ट्रिंग डेटा में स्पेसिफिक कैरेक्टर तक सीधे एक संख्यात्मक इंडेक्स या की वैल्यू का उपयोग करके पहुँचा जा सकता है। इस प्रक्रिया को इंडेक्सिंग के रूप में संदर्भित किया जाता है।

In Python, to access individual characters in a string, you need specify string name followed by a number in square brackets ([]).

पायथन में, एक स्ट्रिंग में अलग-अलग कैरेक्टर तक पहुँचने के लिए, आपको square brackets ([]) में एक नंबर के बाद स्ट्रिंग नाम निर्दिष्ट करना होगा।

Python supports both positive and negative index.

पायथन पॉजिटिव और निगेटिव दोनों तरह के इंडेक्स का समर्थन करता है।

Positive index – पॉजिटिव इंडेक्सः

Positive index means starts from left to right (forward direction). The first character in the string has index 0, the next has index 1, and so on. The index of the last character will be the length of the string minus one.

पॉजिटिव इंडेक्स का मतलब बाएं से शुरू होकर दायें (आगे की दिशा) में होता है। स्ट्रिंग में पहले कैरेक्टर में इंडेक्स 0 होता है, और इसी तरह अगले में इंडेक्स 1 होता है। अंतिम कैरेक्टर का इंडेक्स स्ट्रिंग माइनस एक की लंबाई होगी।

Negative index – निगेटिव इंडेक्सः

Negative index means starts from right to left (backward direction). The first character in the string has index -1, the next has index -2, and so on. The index of the last character will be the length of the string. निगेटिव इंडेक्स का मतलब दाएं से बाएं (पीछे की ओर) से शुरू होता है। स्ट्रिंग में पहले कैरेक्टर में सूचकांक -1 है, अगले में सूचकांक -2 है, और इसी तरह । अंतिम कैरेक्टर का सूचकांक स्ट्रिंग की लंबाई होगी।

For example:
s1 = “pyramid”

Concatenation – संयोजन

In any language contains merging or combining strings. For example, one string would be “Python” and the other would be “Programming”. When you use to combine them it becomes one string, or “Python Programming”. This process is nothing but “concatenation”. In Python, there are a several ways to concatenate or combine – strings.

किसी भी भाषा में स्ट्रिंग का मर्जिंग या संयोजन होता है। उदाहरण के लिए, एक स्ट्रिंग “पायथन” होगा और दूसरा “प्रोग्रामिंग” होगा। जब आप उन्हें संयोजित करने के लिए उपयोग करते हैं तो यह एक स्ट्रिंग, या “पायथन प्रोग्रामिंग” बन जाता है। यह प्रक्रिया कुछ भी नहीं है लेकिन “concatenation” है। पायथन में कई तरीके हैं जिन्हें समेटना या संयोजित करना – स्ट्रिंग करना है।

In Python, if want to apply ‘+’ operator for the string, compulsory both arguments should be string type only otherwise PVM throws an error message.

पायथन में, यदि स्ट्रिंग के लिए ‘+’ ऑपरेटर को लागू करना चाहते हैं, तो अनिवार्य दोनों तर्क स्ट्रिंग टाइप होने चाहिए अन्यथा पीवीएम एक एरर मैसेज देता है।

For example:

>>> strl = "Python" 
>>> str2 = "Programming" 
>>> strl + str2 
'PythonProgramming'
>>> print (strl + str2) 
PythonProgramming

Note: Python cannot concatenate a string with integer.

पायथनइन्टिजर के साथ एकस्ट्रिंग को जोड़ नहीं सकता है ।

For example:

>>> str1 = "Hello" 
>>> val = 100 
>>> str1 + val 
Traceback (most recent call last): 
File "<pyshell#8>", line 1, in <module>
str1 + val 
TypeError: can only concatenate str (not "int") to str

To make this possible, we can convert the number into a string using the appropriate function.

इसे संभव बनाने के लिए, हम उचित फंक्शन का उपयोग करके संख्या को एक स्ट्रिंग में बदल सकते हैं।

For example:

>>> str1 + str (val) 
'Hello100'
>>> print (str1 + str (val))
Hello100

The method we used to do this is the str() function. The str() function converts the specified value into a string.

जिस विधि से हम ऐसा करते थेवहstr() फंक्शन है।str() फंक्शन निर्दिष्ट मान को एक स्ट्रिंग परिवर्तित करता है ।

Note: In Python, if want to apply ‘*’ operator (string repetition operator) for the string, compulsory one argument should be string and other one must and should integer type only otherwise PVM throws an error message.

पायथन में, यदि आप स्ट्रिंग के लिए ‘*’ ऑपरेटर (स्ट्रिंग दोहराव ऑपरेटर) लागू करना चाहते हैं, तो अनिवार्य एक तर्क स्ट्रिंग और अन्य एक होना चाहिए और इंटीगर प्रकार ही होना चाहिए अन्यथा पीवीएम एक एरर मैसेज देता है।

For example:

>>> "Python " * 3 
'Python Python Python '
>>> 3 * " Program" 
' Program Program Program'
>>> "hello" * "hello" 
Traceback (most recent call last): 
File "<pyshell#13>", line 1, in <module>
"hello" * "hello" 
TypeError: can't multiply sequence by non-int of type 'str'.

In order to merge two strings into a single object, you may use the format () method.

किसी एक ऑब्जेक्ट में दो स्ट्रिंग मिलाने के लिए, आप फार्मेट() पद्धति का उपयोग कर सकते हैं।

The format () method takes the passed arguments, formats them, and places them in the string where the placeholders {} are:

फार्मेट() मेथडे पारित तर्कों को लेती है, उन्हें प्रारूपित करती है, और उन्हें स्ट्रिंग में रखती है जहां प्लेसहोल्डर {} हैं।

For example: Use the format () method to insert numbers into strings.

उदाहरण के लिए: संख्याओं को स्ट्रिंग्स में सम्मिलित करने के लिए फार्मेट()पद्धति का उपयोग करें।

>>> ver = 3.8
>>> txt = "Python\'s latest version is {}" 
>>> print (txt.format (ver)) 
Python's latest version is 3.8

The format () method takes unlimited number of arguments, and are placed into the respective placeholders.

फार्मेट()मेथड असीमित संख्या में तर्क लेती है, और उन्हें संबंधित प्लेसहोल्डर्स में रखा जाता है।

For example:

>>> eMarks = 90 
>>> hMarks = 99 
>>> mMarks = 100 
>>> sName = "Python" 
>>> avgMarks = (eMarks+hMarks+mMarks)/3 
>>> txt = "I am {}, my english Marks {}, hindi marks {}, Maths Marks {}, finally my average margks {}" 
>>> print (txt.format (sName, eMarks, hMarks, mMarks, avgMarks)) 
I am Python, my english Marks 90, hindi marks 99, Maths Marks 100, finally my average margks 96.33333333333333

You can use index numbers {0} to be sure the arguments are placed in the correct placeholders.

यह सुनिश्चित करने के लिए कि सही प्लेसहोल्डर्स में तर्कों को रखा गया है, इंडेक्स नंबर {0} का उपयोग कर सकते हैं।

For example:

>>> RollNo = 10 
>>> Name = "Python" 
>>> Marks = 890 
>>> result =" Student Result -- Name : {1}, RollNo : {0} and Marks : {2}" 
>>> print (result.format (RollNo, Name, Marks) )
Student Result -- Name : Python, RollNo : 10 and Marks : 890

Chapter wise Model Paper Link

About Me