🟢 Beginner · Lesson 15
String Handling in Python
Python में String Handling
What is a String?
A string is text — a sequence of characters inside quotes. Strings are immutable (cannot be changed in place); any "change" creates a new string.
Python – str.py
name = "CodeKaFunda" print(name) print(len(name)) # number of characters
CodeKaFunda
11
Indexing & Slicing
Each character has an index starting at 0. Negative indexes count from the end.
Python – slice.py
word = "PYTHON" print(word[0]) # first print(word[-1]) # last print(word[0:3]) # characters 0,1,2 print(word[::-1]) # reversed
P
N
PYT
NOHTYP
word[0]= first char;word[-1]= last char.word[0:3]= slice from 0 up to (not including) 3.[::-1]reverses the whole string.
Useful String Methods
| Method | Does | Example |
|---|---|---|
| .upper() | UPPERCASE | "hi".upper() → HI |
| .lower() | lowercase | "HI".lower() → hi |
| .strip() | remove spaces | " hi ".strip() → hi |
| .replace() | swap text | "cat".replace("c","b") → bat |
| .split() | break into list | "a b".split() → [a, b] |
| .find() | position of text | "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()makes the check case-insensitive.word[::-1]reverses it; if it equals the original, it is a palindrome.
Program 3: Count 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
The loop checks each character; ch in "aeiou" is True only for vowels.
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() breaks the sentence at spaces into a list; len() counts the items.
Common Mistakes
- Trying to change a character:
s[0] = "X"fails (strings are immutable). - Forgetting
.lower()before comparing text. - Confusing index (position) with the character itself.
Practice Tasks
- Ask for a name and print it in uppercase and reversed.
- Count how many times a letter appears in a word.
- Check if a sentence is a palindrome (ignore spaces).
- Replace all spaces in a string with underscores.
Summary
- Strings are immutable sequences of characters; index starts at 0.
- Slicing
[start:stop:step]extracts parts;[::-1]reverses. - Common methods: upper, lower, strip, replace, split, find.
String क्या है?
String text है — quotes के अंदर characters का sequence। Strings immutable हैं (जगह पर बदली नहीं जा सकतीं); कोई भी "बदलाव" नई string बनाता है।
Python – str.py
name = "CodeKaFunda" print(name) print(len(name)) # characters की संख्या
CodeKaFunda
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।