If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Python में String series या group of characters को string कहते हैं। बैसे तो Python में almost सब Object ही है , इसलिए string भी str class से ही belong करती है।
Python में हम string 3 तरह से define कर सकते हैं।
s1 = 'sigle quote string'
s2 = "double quote string"
s3 = str("String using str.")
#check type.
print('Type of s1 :', type(s1))
print('Type of s2 :', type(s2))
print('Type of s3 :', type(s3))
C:\Users\Rahulkumar\Desktop\python>python str_ex.py Type of s1 : <class 'str'> Type of s2 : <class 'str'> Type of s3 : <class 'str'>
जैसा कि आप example में आप देख सकते हैं , तीनो में से आप कोई भी तरीका use कर सकते हैं।
हालाँकि अगर आप ऊपर define किये तरीके से आप सिर्फ single line string ही define कर सकते हैं , अगर multiline लिखने की कोशिश तो error आएगी।
s = "this is a multiline string , it will raise an error" #it will raise an error. File "<str_ex.py>", line 1 s = "this is a multiline ^ SyntaxError: EOL while scanning string literal
Multiline Line String लिखने के लिए Python में triple single quote (''') और triple double quote (""") का use करते हैं। String को आप जैसा लिखोगे वैसी ही print हो जायगी।
s1 = """this is a double quoted
multiline string"""
#now define using single quotes.
s2 = '''this is a single quoted
multiline string'''
print(s1)
print(s2)
C:\Users\Rahulkumar\Desktop\python>python multi_str_ex.py this is a double quoted multiline string this is a single quoted multiline string
किसी भी string की length जानने के लिए len() और type जानने के लिए type() function का use किया जाता है।
For Example -
s1 = "Welcome !"
#define using single quotes.
s2 = 'hello !'
print('type of s1 :', type(s1))
print('length of s1 :', len(s1))
print('type of s2 :', type(s2))
print('length of s2 :', len(s2))
C:\Users\Rahulkumar\Desktop\python>python str_ex.py type of s1 : <class 'str'> length of s1 : 9 type of s2 : <class 'str'> length of s2 : 7
Python में String as a Array / list की तरह से work करती है , मतलब String के सभी characters का Array की तरह ही एक index number होता है जो कि 0 से start होता है।
इन index number के help से आप किसी String का particular character access कर सकते हैं। या list की तरह एक particular part भी access कर सकते हैं।
See Example -
s = "Hello world !"
#access a character.
print(s[4])
#access a part of string.
#from the index number 6.
print(s[6:])
#negeative index.
print(s[-1])
print(s[-7:])
C:\Users\Rahulkumar\Desktop\python>python str_ex.py o world ! ! world !
Note : list की तरह आप सिर्फ किसी particular character को सिर्फ access कर सकते हैं , update या delete नहीं कर सकते हैं।
इसी तरह से किसी भी String पर while loop या for loop की help से iterate भी कर सकते हैं।
For Example -
s = "Welcome"
for char in s :
print(char)
C:\Users\Rahulkumar\Desktop\python>python str_iterate.py W e l c o m e
I Hope, अब आपको Python में String अच्छे से समझ आ गया होगा।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook