🟢 Beginner · Lesson 15
String Handling in Python
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 = "CodingEasily" print(name) print(len(name)) # number of characters
CodingEasily
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.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.