
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Python String : Single and double quote के अंदर हम कुछ लिखते हैं तो यह string कहलाता है.
Triple quote के अंदर हम कुछ लिखते हैं तो भी यह string कहलाता है इसका use हम multi line string के लिए करते हैं.
print("this is single line string")
print("""this is
multi-line
string""")
अगर हम string में से किसी specific data को print करना चाहते हैं तो indexing का use करते हैं.
python string में characters का indexing 0 से start होता है, और negative indexing end से start होता है -1 last character को represent करता है.
text = "hello, world!"
print(text[0])
print(text[-1])
#output
# h
# !
slicing का use string के Sab string पाने के लिए होता है,
start: Ye wo index hai jahan se slicing start hoti hai.
stop: Ye wo index hai jahan tak slicing hoti hai.
step: Ye optional parameter hai jo batata hai ki kitne steps ka difference rakhna hai.
Agar aap start, stop, ya step ko nahi dete, to default values use hoti hain:
Default value for start is 0.
Default value for stop is length of the string.
Default value for step is 1
text = "hello, world!"
print(text[0:5])
print(text[7:])
print(text[:5])
print(text[-6:-1])
print(text[0:5:2])
#output
# hello
# world!
# hello
# world
# hlo
string concatenation का मतलब होता है एक या एक से अधिक strings को add करके single string मे convert करना।
string को concatenate करने के लिए + operator या join methode का use किया जाता है।
a = "python"
b = "programing"
c = "hub"
# using + operator
print(a +" "+ b +" "+ c)
# using join methode
print(" ".join([a,b,c]))
# output
# python programing hub
# python programing hub
NOTE: string concatenation मे हमने double quote ( ” ” ) का use इसलिए किया है ताकि हम strings के बीच मे space दे सके अगर double quote नहीं लगाएंगे तो सारे strings एक मे mix होकर single word बन जाएंगे।
अगर आप double quote के अंदर कुछ लिखेंगे तो वो भी string मे join हो जाएगा।
अगर हम चाहते है कि string मे variables या expressions के value को direct insert कर सके तो हमे string formatting का उसे करना होगा। python मे string formatting बनाने के लिए % operator, str.format() method और f-strings का use करते है।
name = "Lisa"
age = 25
print("Hello my name is %s and I am %d year old." %(name, age))
name = "Lisa"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
# Output: My name is Lisa and I am 25 years old.
name = "Lisa"
age = 25
print(f"My name is {name} and I am {age} years old.")
# Output: My name is Charlie and I am 35 years old.
# Aap expressions bhi use kar sakte hain
print(f"Next year, I will be {age + 1} years old.")
# Output: Next year, I will be 26 years old.
python मे string के लिए कई सारे build-in methods होते है जिन्हे use करने पर string अलग अलग तरीके से perform करता है।
Syntex:
method को किसी भी variable के साथ use करने के लिए dot ( . ) का use करते है।
Example:
str.upper() : इस method का use करके हम string के सभी lower case letters को upper case मे कर सकते है।
text = "hello"
result = text.upper()
print(result)
# Output: HELLO
upper(): String को uppercase मे convert करता है।
lower(): String को lowercase मे convert करता है।
capitalize(): पहला letter uppercase करता है, बाकी lowercase
title(): हर word का पहला letter uppercase करता है।
strip(): Start और end के whitespace (या characters) remove करता है।
lstrip(): Left से whitespace (या characters) remove करता है।
rstrip(): Right से whitespace (या characters) remove करता है।
replace(old, new): String मे किसी substring को replace करता है।
split(separator): String को list मे break करता है।
str.join(iterable): List (या iterable) को string मे add करता है।
str.find(sub): Substring का index return करता है (अगर नहीं मिल तो -1 return करेगा)
str.index(sub): Substring का index देता है।
str.count(sub): Substring कितनी बार आया है, वो count करता है।
str.isalpha(): Check करता है की सब characters alphabets है या नहीं अगर है तो true return करेगा।
str.isdigit(): Check करता है की सभी characters digits है या नहीं अगर है तो true return करेगा।
str.isalnum(): Alphabets + Digits है तो True return करता है ।
str.isspace(): अगर string मे सिर्फ whitespace है तो True return करेगा।
str.islower(): Check करता है की string lowercase मे है या नहीं। अगर है तो true return करेगा।
str.isupper(): Check करता है की string uppercase मे है या नहीं, अगर है तो true return करेगा।
उम्मीद करते है कि आपको python string अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...