Strings in Python

Strings in Python
Strings are sequences of character data. To store collection of characters, then we use string data type. The string data type in Python is called str and str is one of the class treated as data type.

स्ट्रिंग्स कैरेक्टर डेटा के सिक्वेंस हैं। कैरेक्टर का कलेक्शन स्टोर करें, फिर हम स्ट्रिंग डेटा टाइप का उपयोग करते हैं। पायथन में स्ट्रिंग डेटा टाइप को str कहा जाता है और str को डेटा प्रकार के रूप में माना जाने वाला एक वर्ग है।

A string enclosed within ‘single quotes’ or “double quotes”.

एक स्ट्रिंग ‘single quotes’ या “double quotes” के भीतर संलग्न होता है।

For example:

>>> s1='python programming' 
>>> s2="So simple" 
>>> s1 
'python programming' 
>>> s2
'So simple' 
>>> type (s1) 
<class 'str'>
>>>print (s2) 
So simple

Remember, in Python by using single or double quotes we cannot represent multi line string literals. We can only represent single line string literals.

याद रखें, पायथन में सिंगल या डबल कोट्स का उपयोग करके हम मल्टी लाईन स्ट्रिंग लिटरल का प्रतिनिधित्व नहीं कर सकते हैं। हम केवल सिंगल लाइन स्ट्रिंग लिटरल का प्रतिनिधित्व कर सकते हैं।

For example:

>>> s1="python 
SyntaxError: EOL while scanning string literal 
>>> s1='python
SyntaxError: EOL while scanning string literal 

In order to represent multi line string literals, we should go for triple single quotes (”) or triple double quotes (“””).

मल्टी लाईन स्ट्रिंग लिटरल का प्रतिनिधित्व करने के लिए, हमें ट्रिपल सिंगल कोट्स (”) ट्रिपल डबल कोट्स (“””) के लिए जाना चाहिए।

For example:

>>> s1 =''' Python is general purpose high level programming language''' 
>>> s1 
' Python is general purpose\nhigh level programming language'
>>> s2= """ Python is general purpose high level programming language""" 
>>> s2
' Python is general purpose high level programming language'

To display a string with ‘single quotes’ or “double quotes” take a look at the below example;

‘सिंगल कोट्स’ या “डबल कोट्स” के साथ एक स्ट्रिंग प्रदर्शित करने के लिए नीचे दिए गए उदाहरण पर एक नजर डालें:

>>> s1 = "hi, i am your 'python' programming language" 
>>> print (s1) 
hi, i am your 'python' programming language
>>> s1 = 'hi, i am your "python" programming language' 
>>> print (s1) 
hi, i am your "python" programming language

To display ‘single quote’ and “double quotes” in a string, take a look at the below example.

एक स्ट्रिंग में ‘सिंगल कोट्स’ या “डबल कोट्स” प्रदर्शित करने के लिए नीचे दिए गए उदाहरण पर एक नजर डालें:

>>> s1= 'hi 'ram', I am your "Python" language' 
SyntaxError: invalid syntax 
>>> s1= "hi 'ram', I am your "Python" language" 
SyntaxError: invalid syntax 
>>> s1= ''' hi 'ram', I am your "Python" language''' 
>>> print (s1)
hi 'ram', I am your "Python" language
>>> s1= """hi 'ram', I am your "Python" language""" 
>>> print (s1) 
hi 'ram', I am your "Python" language

So that, there is a question in Python, where we use triple quotes?

तो, एक सवाल है कि पायथन में, कहाँ हम ट्रिपल कोट्स का उपयोग करते हैं?

In Python, we use triple quotes in different ways:

पायथन में, हम अलग-अलग तरीकों से ट्रिपल कोट्स का उपयोग करते हैं:

Slicing – स्लाईसिंग

In a string, if you want to get a part of that string, that part is nothing but a slice. In this case we use to get part of the string using slice operator.

एक स्ट्रिंग में, यदि आप उस स्ट्रिंग का एक हिस्सा प्राप्त करना चाहते हैं, तो वह हिस्सा एक स्लाइस के अलावा कुछ भी नहीं है। इस मामले में प्रत्येक स्लाइस ऑपरेटर का उपयोग करके स्ट्रिंग का हिस्सा प्राप्त करने के लिए उपयोग करते हैं।

In other words, string slicing is mechanism of obtaining a part of a string or range of characters from the given string.

दूसरे शब्दों में स्ट्रिंग स्लाइसिंग स्ट्रिंग के एक भाग या दिए गए स्ट्रिंग से कैरेक्टर का एक भाग प्राप्त करने का मैकेनिजम है।

Syntax: strObj [ begin : end : step ]

Begin: Starting index where the slicing of the object begins.

इंडेक्स शुरू करना जहां ऑब्जेक्ट का स्लाईसिंग करना शुरू होता है।

End: Integer until which the slicing takes place. The slicing ends at index end – 1.

इंटिगर जब तक स्लाईसिंग करने की क्रिया होती है। इंडेक्स end – 1 में स्लाइसिंग समाप्त होता है।

Step:Increment value. – इंक्रीमेंट वैल्यू

For example:

>>> s1 = "PYRAMID"
>>>print(s1 [2:5])
RAM 
#s1[2:5] returns a substring (slice) from begin index 2 to end-1(5-1) index i.e., ‘RAM'
>>>print(s1 [1:4]) 
YRA

Note:

  1. If you don’t specify any begin index value then it will consider from beginning of the string.

यदि आप किसी आरंभिक इंडेक्स वैल्यू को निर्दिष्ट नहीं करते हैं तो यह स्ट्रिंग की शुरुआत से विचार करेगा।

  1. If you don’t specify any end index value then will consider up to end of the string.

यदि आप किसी इंड इंडेक्स वैल्यू को निर्दिष्ट नहीं करते हैं तो स्ट्रिंग के अंत तक विचार करेंगे।

  1. If you don’t specify value of begin & end index then total string will be taken by default.

यदि आप आरंभ और अंत इंडेक्स का मान निर्दिष्ट नहीं करते हैं तो कुल स्ट्रिंग डिफॉल्ट रूप से लिया जाएगा।

In Python, slice operator while using forward direction:
पायथन में, आगे की दिशा का उपयोग करते समय स्लाइस ऑपरेटर:

1. Default value for begin index is ‘0’

आरंभिक इंडेक्स का डिफॉल्ट वैल्यू ‘0’ है।

2. Default value for end index is ‘length of string’

इंड इंडेक्स के लिए डिफॉल्ट मान पे स्ट्रिंग की लम्बाई है।

3. Default value for step is ‘1’

स्टेट के लिए डिफॉल्ट वैल्यू ‘1’ है।

In Python, slice operator while using backward direction:
पायथन में, आगे पीछे की दिशा का उपयोग करते समय स्लाइस ऑपरेटर:

1. Default value for begin index is ‘-1’

आरंभिक इंडेक्स का डिफॉल्ट वैल्यू -1 है।

2. Default value for end index is ‘length of string+1’

इंड इंडेक्स के लिए डिफॉल्ट वैल्यू ‘length of string+1’ है।

3. Default value for step is ‘-1’

स्टेप के लिए डिफॉल्ट वैल्यू -1′ है।

Chapter wise Model Paper Link

About Me