Home >> Python Tutorial >> Python String
Edit Template
Edit Template

Python String in Hindi

What is Python String

Python String : Single and double quote के अंदर हम कुछ लिखते हैं तो यह string कहलाता है.

Triple quote के अंदर हम कुछ लिखते हैं तो भी यह string कहलाता है इसका use हम multi line string के लिए करते हैं.

🐍
string.py
Copy to clipboard
print("this is single line string")
print("""this is
multi-line
string""")

String Indexing

अगर हम string में से किसी specific data को print करना चाहते हैं तो indexing का use करते हैं.

 python string में characters का indexing 0 से start होता है, और negative indexing end से start होता है  -1 last character को represent करता है.

🐍
string indexing.py
Copy to clipboard
text = "hello, world!"
print(text[0])
print(text[-1])
#output
#   h
#   !

String Slicing

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

🐍
string slicing.py
Copy to clipboard
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

string concatenation का मतलब होता है एक या एक से अधिक strings को add करके single string मे convert करना। 

string को concatenate करने के लिए + operator या join methode का use किया जाता है।

🐍
string concatenation.py
Copy to clipboard
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 Formatting

अगर हम चाहते है कि string मे variables या expressions के value को direct insert कर सके तो हमे string formatting का उसे करना होगा। python मे string formatting बनाने के लिए % operator, str.format() method और f-strings का use करते है। 

% Operator

🐍
formatting 1.py
Copy to clipboard
name = "Lisa"
age = 25
print("Hello my name is %s and I am %d year old." %(name, age))

str.format Method

🐍
formatting 2.py
Copy to clipboard
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.

f-strings

🐍
formatting 3.py
Copy to clipboard
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.

String Methods

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 मे कर सकते है। 

🐍
string method.py
Copy to clipboard
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 करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
Dunder Methods

Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods   Python Tutorial Python Introduction Identation &...

Encapsulation

Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers      2.1. Public Members      2.2. Protected Members...

Lambda Function

Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...

Nested Function

Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...

Context Manager

Table of Contents All Chapters 1. Context Manager 2. with Statement      2.1. __enter__() Method      2.2. __exit__()...

File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Edit Template
Scroll to Top