Python में String series या group of characters को string कहते हैं। बैसे तो Python में almost सब Object ही है , इसलिए string भी str class से ही belong करती है।


Python में हम string 3 तरह से define कर सकते हैं।

  1. single quote string.
  2. double quote string.
  3. using str() function.

Python String Example

Copy Fullscreen Close Fullscreen Run
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))
Output
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 कर सकते हैं।

Python Multiline String

हालाँकि अगर आप ऊपर 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 हो जायगी।

Python Multiline String Example
Copy Fullscreen Close Fullscreen Run
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)
Output
C:\Users\Rahulkumar\Desktop\python>python multi_str_ex.py
this is a double quoted 
multiline string
this is a single quoted 
multiline string

Python String type() & len()

किसी भी string की length जानने के लिए len() और type जानने के लिए type() function का use किया जाता है।
For Example -

Copy Fullscreen Close Fullscreen Run
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))
Output
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 Works Like Array

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 -

Copy Fullscreen Close Fullscreen Run
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:])
Output
C:\Users\Rahulkumar\Desktop\python>python str_ex.py
o
world !
!
world !

Note : list की तरह आप सिर्फ किसी particular character को सिर्फ access कर सकते हैं , update या delete नहीं कर सकते हैं।


Python Iterating String

इसी तरह से किसी भी String पर while loop या for loop की help से iterate भी कर सकते हैं।
For Example -

Copy Fullscreen Close Fullscreen Run
s = "Welcome"
for char in s :
    print(char)
Output
C:\Users\Rahulkumar\Desktop\python>python str_iterate.py
W
e
l
c
o
m
e

I Hope, अब आपको Python में String अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers