Python in Hindi - Python Strings

एक string characters की sequence होती है। यानी की जब characters को एक क्रम में organize किया जाता है तो वे string बन जाते है।
Python में सब कुछ object है और एक string भी object ही होती है। Python में strings immutable objects होती है। यानि एक बार string create करने के बाद आप उसे change नहीं कर सकते है। किसी भी दूसरे variable की तरह del command से आप string को भी delete कर सकते है।
Python में string create करने के लिए आप एक variable create करते है और single या double quotes में string को लिखकर उसे assign करवाते है।
उदाहरण के लिए निचे दिए गए code में एक string message create किया गया है।  



message = “Hello Reader”
Python में strings को create करते समय आप single या double कोई से भी quotes use कर सकते है। Python में ये दोनों same ही माने जाते है। यानि की ऊपर दिए गए code को इस प्रकार भी लिखा जा सकता है।
message = ‘Hello Reader’
Python में character data type available नहीं है। Python में character create करने के लिए आप single character से string create करते है। उदाहरण के लिए निचे दिए गए code को देखिये।
language = “C”
Python में strings को print() function के द्वारा screen पर output किया जा सकता है। उदाहरण के लिए निचे दिए गए code में एक string को print function द्वारा screen पर show किया जा रहा है।
print(message)

यह भी पढ़े - Python If-else Statements

दूसरी modern programming languages की तरह python में भी strings array के रूप में represent की जाती है। String के elements को index numbers assigned होते है जिनके द्वारा उन्हें access किया जा सकता है। इसके लिए विभिन्न operators का प्रयोग किया जाता है जिनके बारे में आप आगे जानेंगे।

Python String Operators

Python में strings के साथ कार्य करने के लिए कुछ operators provide किये गए है। इनके बारे में आगे बताया जा रहा है।

+

यह operators दो strings को जोड़ने के लिए use किया जाता है। उदाहरण के लिए निचे दिए गए code को देखिये।
msg = “Hello”
name = “Reader”

print msg+name
ऊपर दिए गए उदाहरण में + operator द्वारा msg और name strings को concatenate करके print करवाया गया है।

*

यह किसी भी string को number of times print करने के लिए use किया जाता है। उदाहरण के लिए निचे दिए गए code को देखिये।
msg = “Hello”
print msg*2
ऊपर दिए गए उदाहरण में * operator के द्वारा msg string को 2 बार print करवाया गया है।


[]

यह string के elements को access करने के लिए प्रयोग किया जाता है। उदाहरण के लिए string के first element को access करने के लिए आप इसे इस प्रकार use कर सकते है।
msg = “Hello Python”
print(msg[0])
ऊपर दिए गए उदाहरण में Hello Python string के first element H को print करवाया गया है। एक बात आपको हमेशा ध्यान रखनी चाहिए की python में किसी भी array की तरह string elements की index भी zero से शुरू होती है। यानि की string का first element [0] position पर होगा।

यह भी पढ़े- Python Web Developer कैसे बने?

[ : ]

यह किसी string से substring extract करने के लिए प्रयोग किया जाता है। इसमें colon के एक तरफ आप substring की starting position लिखते है और दूसरी और ending position लिखते है। इसके बाद execute होने पर उन position के बीच की substring return की जाती है।
उदाहरण के लिए निचे दिए गए code को देखिये।
msg = “Hello Python”

print(msg[0:4])
ऊपर दिए गए उदाहरण में Hello Python string से Hello substring को print करवाया गया है।

in

यह operator किसी string में कोई character है या नहीं यह पता लगाने के लिए प्रयोग किया जाता है। यदि वह character available है तो true (1) return किया जाता है। उदाहरण के लिए निचे दिए गए code को देखिये।
msg = “Hello Python”
print “o” in msg 
ऊपर दिए गए उदाहरण में क्योंकि Hello Python string में o available है इसलिए true return किया जायेगा।

not in 

यह operator in operator के विपरीत कार्य करता है। यानि की in operator जँहा string में कोई character available होने पर true return करता है वँही दूसरी तरफ not in operator string में कोई character नहीं होने पर true return करता है।
उदाहरण के लिए निचे दिए गए code को देखिये।
msg = “Hello Python”
print “a” not in msg


ऊपर दिए गए code को execute किये जाने पर true return होगा क्योंकि a Hello Python string में available नहीं है।

%

यह operator string formatting के लिए प्रयोग किया जाता है। इस operator के साथ मुख्यतः d और s specifiers को use किया जाता है। इसमें %d तो numbers को format करके display करवाने के लिए और %s को string को display करवाने के लिए use किया जाता है। इसका उदाहरण निचे दिया जा रहा है।
name = “Best Hindi Tutorials”
Age = 100

print(“%s, %d”,name,Age)
ऊपर दिए गए उदाहरण में % operator के द्वारा name और Age print करवाई गयी है।
* String  के Built in Functions :
1.  len() function:
इस फंक्शन का use कर के string की length  को count कर सकते है, और पता लगा सकते है कि string के अंदर कितने elements है।
Example
Str = "hello python"
print len(str)

Output 12

2.  Capitalize() function:
इस फंक्शन का use कर के string के first letter को capitalize किया जाता है। (only first) जहाँ से string start होती है। 
Example
Str = "hello python"
print str.capitalize()

Output Hello python


3.  Lower() function:
इस फंक्शन का use कर के string के letters को  Small letters में बदल लिया जाता है। 
Example
Str = "HELLO PYTHON"
print str.lower()

Output hello python



4.  Upper() function:
इस फंक्शन का use कर के string के letters को  Capital letters में बदल लिया जाता है। 
Example
Str = "hello python"
print str.upper()

Output HELLO PYTHON


5.  Replace(old, new) function:
इस फंक्शन का use कर के string के letters और words को दूसरे words या letters से बदल(replace) लिया जाता है। 
Example
Str = "HELLO PYTHON"
print str.replace("PYTHON" ,"CODEVIDYA")

Output HELLO CODEVIDYA


6.  Join(seq) function:
इस फंक्शन का use  दो strings को एक साथ join करने के लिए किया जाता है। 
Example
Str= " "
Seq = "HELLO PYTHON"
print str.join(seq)

Output HELLO PYTHON

ऊपर दिए हुए EXAMPLE में हमने Str को print करवाया है। जबकि शुरुवात में str के अंदर कोई भी element नही डाला हुआ था। हमने join फंक्शन का use करके seq वाली string को str के साथ join किया। जिससे seq के element str के साथ जुड़ गए। तथा str को print कराने पर हमें यह output प्राप्त हुआ।

7.  title() function:
इस फंक्शन का use कर के string के elements का means  हर word का first letter Capital कर दिया जाता है।
Example
Str = "this is python script"
print str.title()

Output This Is Python Script



8.  swapcase() function:
इस फंक्शन के द्वारा हम string के Case को change कर सकते है। means अगर आप की string capital letters में लिखी हुई है, तो यह उसको small letters में बदल देता है। अगर small letters में हो तो उसे capital letters में बदल देता है। यानी की swap फंक्शन string के elements के case को swap कर देता है।
Example
Str = "this is python script"
print str.swapcase()
Output THIS IS PYTHON SCRIPT

Str= "THIS IS PYTHON SCRIPT"
print str.swapcase()
Output this is Python script

Str= "This Is Python Script"
print str.swapcase()
Output tHIS iS pYTHON sCRIPT



9.  encode() और decod() function:
इस फंक्शन के द्वारा हम string को encryption और decryption करने के लिए use में लेते है। साधारण भाषा में समझे तो इसका use data को secure करने के लिए किया जाता है।  encode यानी encryption और decode यानी decryption।
Example
password= "demo@1234"
print "Original password is :" ,password

Output Original password is : demo@1234

#encrypting the password
encode= password. encode('base64' , 'strict')
print "After encoding the password : " ,encode

Output   After encoding the password : ZGVtb0AxMJM0NTY=


#decrypting the password
decode= encode.decode('base64' , 'strict')
print "After decoding the password : " ,decode

Output   After decoding the password : demo@1234




Python String Methods:

Python में str एक built in class है। यह class strings के साथ work करने के लिए important methods provide करती है। क्योंकि python में सभी strings objects होती है। इसलिए आप str class के methods को strings से आसानी से call कर सकते है। 

strip()

यह method string के beginning या end से whitespace को remove करता है। उदाहरण के लिए निचे दिए गए code को देखिये।
msg = ” Good Morning “
print(msg.strip())
ऊपर दिए गए उदाहरण में string को create करते समय पहले और बाद में space दिया गया है। Strip method के execute किये जाने पर यह space remove हो जाता है।

split()

यह method string को split करता है। इस method में आप argument के रूप में एक separator pass करते है। इसी separator के आधार पर string को split किया जाता है। उदाहरण के लिए निचे दिए गए code को देखिये।
msg = “Good, Morning”
print(msg.split(“,”))
ऊपर दिए गए उदाहरण में string Good, Morning को comma के आधार पर split() method द्वारा separate किया गया है।

isalpha()

यह method test करता है की क्या string alphanumeric है। यदि string alphanumeric है तो true return किया जायेगा। इसका उदाहरण निचे दिया जा रहा है।
msg = “70 Independence Day”
print(msg.isalpha())
ऊपर दिए गए उदाहरण में क्योंकि msg एक alphanumeric string है इसलिये true return होगा।

isdigit()

<
यह method test करता है की क्या एक string digit है। Digit होने पर true return किया जाता है। इसका उदाहरण निचे दिया जा रहा है।
num = “99”
print(msg.isdigit())
ऊपर दिए गए उदाहरण में num एक digit string है इसलिए true (1) print किया जायेगा।

isspace()

यह method test करता है की क्या कोई specific string space character है। यदि string एक space character है तो true return किया जायेगा। इसका उदाहरण निचे दिया जा रहा है। 
msg = ” “
print(msg.isspace())
ऊपर दिए गए उदाहरण में msg एक space string है इसलिए true return किया जायेगा।

startswith()

यह method check करता है की क्या string किसी pass की गयी string से start होती है। यदि start होती है तो true return किया जाता है। 
msg = “Hello Python”
print(msg.startswith(‘Hello’))
ऊपर दिए गए उदाहरण में क्योंकि msg string Hello से शुरू होती है इसलिए true return किया जायेगा।

find()

यह method इसमें pass की गयी string को call की गयी string में ढूंढता है। यदि वह string मिलती है तो उसकी starting index return की जाती है नहीं तो -1 return किया जाता है। इसका उदाहरण निचे दिया जा रहा है। 
msg = “Hello Python”
print(msg.find(‘Python’))



ऊपर दिए गए उदाहरण में क्योंकि Python string exits करती है इसलिए उसकी starting index return की जाएगी। 

QUIZ QUESTIONS :                                       

1. Write a program to concatenate following strings.

msg1 = “Hello”
msg2 = "Python"

______________________

print msg3 

Output Hello Python
2. Write a program to repeat 5 times following strings.


msg = “Bye”

 print ______________________

Output Bye Bye Bye Bye Bye 


3. Write a program to access string using slice.


msg = “Hello World”

 print ______________________

Output World


4. Write a program to change lower case to upper case of the string msg.


msg = “hello world”

 print ______________________

Output HELLO WORLD


5. Write a program to capitalize the each word of the given string.


msg = “this is the python script”

 print _______________________

Output This Is The Python Script


6. Write a program to encrypt the password string.


password= “1234567890”

 print ______________________

Output MtlzNDUzNzg5MA


7. Write a program to replace the string.


msg = “Chrome is the best browser"

 print ______________________

Output Firefox is the best browser


I hope you understand very well. Here I am providing you some quiz questions for checking your understanding power. If you know the answers ..you can write your answer in comments.. if you have any type of doubt you can also ask me in comment section..I'll reply you..as soon as possible.

Post a Comment

2 Comments

  1. Hiii sir i didn't understand encoding and decoding

    ReplyDelete
    Replies
    1. Dekho..
      password= "demo@1234"
      Isko Hume secure krna hai...taki koi dusra person na dekh ske...means (encryption)encode.

      Encode karne ka ek basic code hota hai.
      encode ('base64','strict')
      Yeh apko likhna pdega...
      Ab jisko encode krna hai. usko iske sath likhdo.
      jaise...
      password.encode('base64','strict')
      yeh Password secure ho Chuka hai. isko hum kisi variable me store krayenge...to variable ka name hum le lete hai... encode...so..
      encode = password.encode('base64','strict')
      ab isko print kra lete h.
      print encode
      (output - MnZl1qar@2SF)
      as it is decode krne k liye.....
      same code likhte hai....lakin decode Hume encode kie hue password Ko he krna hai...or encode Kiya hua password -encode variable me store hai....so hum encode variable Ko he decode krna padega...
      decode krne ka code bhi same hota hai...
      decode('base64','strict')
      so....
      encode.decode('base64','strict')
      yeha password decode ho Chuka hai.
      isko ek variable decode me store kra lete hai.
      decode= encode.decode('base64','strict')
      ab print kra lete hai...decode Ko...
      print decode
      (output - demo@1234)

      Delete