🟢 Beginner · Lesson 15
Python में String Handling
String क्या है?
String text है — quotes के अंदर characters का sequence। Strings immutable हैं (जगह पर बदली नहीं जा सकतीं); कोई भी "बदलाव" नई string बनाता है।
Python – str.py
name = "CodingEasily" print(name) print(len(name)) # characters की संख्या
CodingEasily
11
Indexing और Slicing
हर character का index 0 से शुरू होता है। Negative index अंत से गिनता है।
Python – slice.py
word = "PYTHON" print(word[0]) # पहला print(word[-1]) # आखिरी print(word[0:3]) # characters 0,1,2 print(word[::-1]) # उल्टा
P
N
PYT
NOHTYP
word[0]= पहला char;word[-1]= आखिरी char।word[0:3]= 0 से 3 (शामिल नहीं) तक slice।[::-1]पूरी string उल्टी कर देता है।
उपयोगी String Methods
| Method | क्या करता है | Example |
|---|---|---|
| .upper() | UPPERCASE | "hi".upper() → HI |
| .lower() | lowercase | "HI".lower() → hi |
| .strip() | spaces हटाना | " hi ".strip() → hi |
| .replace() | text बदलना | "cat".replace("c","b") → bat |
| .split() | list में तोड़ना | "a b".split() → [a, b] |
| .find() | text की position | "abc".find("b") → 1 |
Program 1: Length और Case
Python – case.py
text = input("Enter text: ")
print("Length :", len(text))
print("Uppercase :", text.upper())
print("Lowercase :", text.lower())
print("Title :", text.title())Enter text: hello world
Length : 11
Uppercase : HELLO WORLD
Lowercase : hello world
Title : Hello World
Program 2: Reverse और Palindrome Check
Python – palindrome.py
word = input("Enter a word: ").lower()
reversed_word = word[::-1]
if word == reversed_word:
print(word, "is a Palindrome")
else:
print(word, "is NOT a Palindrome")Enter a word: Madam
madam is a Palindrome
.lower()check को case-insensitive बनाता है।word[::-1]उल्टा करता है; original के बराबर हो तो palindrome।
Program 3: Vowels गिनें
Python – vowels.py
text = input("Enter text: ").lower()
count = 0
for ch in text:
if ch in "aeiou":
count += 1
print("Vowels:", count)Enter text: Education
Vowels: 5
Loop हर character check करता है; ch in "aeiou" सिर्फ vowels के लिए True होता है।
Program 4: Word Count
Python – wordcount.py
sentence = input("Enter a sentence: ")
words = sentence.split()
print("Total words:", len(words))
print("Words:", words)Enter a sentence: I love Python programming
Total words: 4
Words: ['I', 'love', 'Python', 'programming']
.split() sentence को spaces पर list में तोड़ता है; len() items गिनता है।
सामान्य गलतियाँ
- character बदलने की कोशिश:
s[0] = "X"fail होता है (strings immutable)। - text compare करने से पहले
.lower()भूलना। - index (position) को character समझ लेना।
Practice Tasks
- नाम पूछकर uppercase और reversed में print करें।
- किसी word में कोई letter कितनी बार आता है, गिनें।
- Check करें कि sentence palindrome है (spaces ignore करें)।
- String के सभी spaces को underscores से replace करें।
सारांश
- Strings characters का immutable sequence हैं; index 0 से शुरू।
- Slicing
[start:stop:step]हिस्से निकालता है;[::-1]उल्टा करता है। - Common methods: upper, lower, strip, replace, split, find।
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.