Chapter 5Python Tutorial~1 min read
Strings in Python
Strings — Text handle करणे
String म्हणजे characters चा sequence — quotes मध्ये लिहिलेला कोणताही text. Python मध्ये single (''), double (""), किंवा triple quotes ("""""" किंवा ''''''') वापरता येतात.
String बनवणे
python
# Different ways to create strings
name = "Rahul"
city = 'Pune'
# Quotes inside string
msg1 = 'He said, "मला Python आवडते!"'
msg2 = "It's a great language!"
# Multi-line string
poem = """
एक होता Python,
सगळ्यांचा favorite,
Data Science मध्ये,
आहे सर्वात vibrant!
"""
print(name)
print(msg1)
print(poem)String Indexing आणि Slicing
Indexing — एक character, Slicing — substring
python
fruit = "Mango"
# 01234
print(fruit[0]) # M — पहिला character
print(fruit[4]) # o — शेवटचा character
print(fruit[-1]) # o — मागून पहिला
print(fruit[-2]) # g — मागून दुसरा
# Slicing — [start:end] — end exclusive!
print(fruit[0:3]) # Man
print(fruit[2:]) # ngo — 2 पासून शेवटपर्यंत
print(fruit[:3]) # Man — सुरुवातीपासून 3 पर्यंत
print(fruit[::-1]) # ognaM — उलटा string!
# len() — length किती?
print(len(fruit)) # 5String Methods
Common string methods
python
text = " Hello World "
# Case methods
print(text.upper()) # " HELLO WORLD "
print(text.lower()) # " hello world "
# Whitespace
print(text.strip()) # "Hello World" — spaces काढतो
print(text.lstrip()) # "Hello World " — left strip
# Search and replace
print(text.replace("World", "Python")) # " Hello Python "
print(text.find("World")) # 8 — index किती? नाही तर -1
# Split
csv = "Pune,Mumbai,Nagpur"
cities = csv.split(",")
print(cities) # ['Pune', 'Mumbai', 'Nagpur']f-Strings — Modern String Formatting
f-strings (Python 3.6+)
python
name = "Rahul"
marks = 85
# Old way — messy
msg1 = "नमस्कार " + name + "! तुम्हाला " + str(marks) + " marks मिळाले."
# f-string — clean and modern!
msg2 = f"नमस्कार {name}! तुम्हाला {marks} marks मिळाले."
print(msg2) # नमस्कार Rahul! तुम्हाला 85 marks मिळाले.
# f-string मध्ये expressions पण लिहिता येतात
print(f"Pass/Fail: {'Pass ✅' if marks >= 40 else 'Fail ❌'}")
print(f"PI = {22/7:.2f}") # PI = 3.14 (2 decimal places)✅ Key Points — लक्षात ठेवा
- ▸String = quotes मध्ये text — single, double, triple
- ▸Indexing 0 पासून सुरू होतो, negative indexing मागून मोजतो
- ▸Slicing: [start:end] — end exclusive
- ▸.upper(), .lower(), .strip(), .replace(), .split() — common methods
- ▸f-string = f"Hello {name}" — modern, clean formatting
0/12 chapters पूर्ण