
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
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. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...
Table of Contents All Chapters 1. Python Set 2. Iterate Set items 3. Set Methods 3.1. Add items...
Table of Contents All Chapters 1. Python Tuple 2. Access Tuple Items 3. Update Tuple 4. Iterate Tuple Items 5....
Table of Contents All Chapters 1. Python List 2. Nested List 3. Range List 4. List Indexing 5. list Methods...
Table of Contents All Chapters 1. Python String 2. String Indexing 3. String Slicing 4. String Concatenation 5. String Formatting...
Table of Contents All Chapters 1. Python Loops 2. While Loop 2.1. Nested While Loop 3. For Loop...
Table of Contents All Chapters 1. If-else Statements 1.1. If Statement 1.2. Else Statement ...