Edit Template
Edit Template
Edit Template

Python String

What is Python String

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 Indexing

अगर हम 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
#   !

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

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 किया जाता है।

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

name = "Lisa"
age = 25
print("Hello my name is %s and I am %d year old." %(name, age))

str.format Method

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

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

text = "hello"
result = text.upper()
print(result)  
# Output: HELLO

यहाँ कुछ methods दिए गए है जिन्हे आप इसी तरह से string के साथ use करके इनके result को देख सकते है। आपको str के जागह अपने string या string variable को use करना है।

MethodDescription
str.upper()String ko uppercase me convert karta hai
str.lower()String ko lowercase me convert karta hai
str.capitalize()Pehla letter uppercase karta hai, baaki lowercase
str.title()Har word ka pehla letter uppercase karta hai
str.strip()Start aur end ke whitespace (ya characters) remove karta hai
str.lstrip()Left se whitespace (ya characters) hatata hai
str.rstrip()Right se whitespace (ya characters) hatata hai
str.replace(old, new)String me kisi substring ko replace karta hai
str.split(separator)String ko list me todta hai
str.join(iterable)List (ya iterable) ko string me jodta hai
str.find(sub)Substring ka index return karta hai (nahi mila to -1)
str.index(sub)Substring ka index deta hai (error deta hai agar na mile)
str.count(sub)Substring kitni baar aaya hai, wo count karta hai
str.startswith(prefix)Check karta hai ki string kisi prefix se start ho rahi hai ya nahi
str.endswith(suffix)Check karta hai ki string kisi suffix se end ho rahi hai ya nahi
str.isalpha()Check karta hai ki sab characters alphabets hain ya nahi
str.isdigit()Check karta hai ki sab characters digits hain ya nahi
str.isalnum()Alphabets + Digits hain to True return karta hai
str.isspace()Agar string me sirf whitespace ho to True
str.islower()Check karta hai ki string lowercase me hai ya nahi
str.isupper()Check karta hai ki string uppercase me hai ya nahi
str.swapcase()Uppercase ko lowercase aur vice versa karta hai
str.zfill(width)String ke left me 0 add karta hai given width tak
str.center(width)String ko center me rakhkar padding deta hai
str.ljust(width)Left justify karta hai
str.rjust(width)Right justify karta hai
str.partition(sep)String ko 3 parts me todta hai (before, sep, after)
str.rpartition(sep)Rightmost separator se partition karta hai

उम्मीद करते है कि आपको python operators अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

Some important questions in this tutorial

What is Python String, Python me String kya hota hai.

Add a comment...

Your email address will not be published. Required fields are marked *

  • 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
Python List

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...

Python String

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...

Python Loops

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...

Conditional Statements

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...

Python Operators

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Python String...

Python Data Type

Python Tutorial Python Introduction Identation & Comments Python Variable Python Data Type Python Operators Conditional Statements Python Loops Hamburger Toggle...

Scikit-learn

Scikit-learn Tutorial Scikit-learn Hamburger Toggle Menu Edit Template Scikit learn in Hindi What is Scikit-learn in Hindi Scikit learn एक...

Pandas

Pandas Tutorial Pandas Hamburger Toggle Menu Edit Template Pandas in Hindi What is Pandas in Hindi pandas  एक python library...

NumPy

NumPy Tutorial NumPy Hamburger Toggle Menu Edit Template NumPy in Hindi What is NumPy in Hindi matplotlib का use हम...

Edit Template
Scroll to Top