Class 11 Practical Programs
Check Palindrome String and Convert Case
AIM
To write a Python program that determines whether a string is a palindrome or not and demonstrates case conversion of characters in a string.
Key Concepts:
- • Palindrome: A string that reads the same forwards and backwards
- • String Slicing: Using [::-1] to reverse a string
- • Case Conversion: Converting between uppercase and lowercase
ALGORITHM
For Palindrome Check:
- Start
- Input a string from the user
- Create a reversed version of the string using slicing [::-1]
- Compare the original string with the reversed string
- If they are equal, the string is a palindrome
- Otherwise, it is not a palindrome
- Display the result
- Stop
For Case Conversion:
- Input a string from the user
- Use built-in string methods for case conversion
- Display original, uppercase, lowercase, and title case versions
PROGRAM
Palindrome Check Program:
# Input a string and determine whether it is a palindrome or not
original = input("Enter a string:")
reverse = original[::-1] # [::-1] will reverse the string
# print("Reverse String=", reverse)
if original == reverse:
print("String is Palindrome:")
else:
print("String is not Palindrome:")Case Conversion Program:
# Convert the case of characters in a string
text = input("Enter a string: ")
print("Original String:", text)
print("Uppercase:", text.upper())
print("Lowercase:", text.lower())
print("Title Case:", text.title())
print("Capitalize:", text.capitalize())
print("Swapcase:", text.swapcase())OUTPUT
Test Case 1 - Palindrome String:
Enter a string: madam
String is Palindrome:
Test Case 2 - Non-Palindrome String:
Enter a string: hello
String is not Palindrome:
Test Case 3 - Case Conversion:
Enter a string: Hello World
Original String: Hello World
Uppercase: HELLO WORLD
Lowercase: hello world
Title Case: Hello World
Capitalize: Hello world
Swapcase: hELLO wORLD
CONCLUSION
In this program, we learned how to check if a string is a palindrome using string slicing and how to perform various case conversions. Key learning outcomes include:
- Understanding string slicing with [::-1] for reversing strings
- String comparison for palindrome detection
- Using built-in string methods for case conversion
- Difference between capitalize(), title(), and other case methods
This program demonstrates fundamental string manipulation techniques that are essential for text processing applications.
VIVA QUESTIONS
What is a palindrome string?
A palindrome string is a string that reads the same forwards and backwards. Examples include "madam", "racecar", "level", etc.
How does [::-1] work in Python?
[::-1] is string slicing syntax where the third parameter -1 indicates step size of -1, which reverses the string by reading it backwards.
What is the difference between upper() and capitalize()?
upper() converts all characters to uppercase, while capitalize() only converts the first character to uppercase and rest to lowercase.
How would you check palindrome ignoring case sensitivity?
Convert both original and reversed strings to the same case using lower() or upper() before comparison: original.lower() == reverse.lower()
What does swapcase() method do?
swapcase() converts uppercase characters to lowercase and lowercase characters to uppercase in the string.
How can you check palindrome without using slicing?
Use a loop to compare characters from start and end positions moving towards center, or use reversed() function.
What is the difference between title() and capitalize()?
title() capitalizes the first letter of each word, while capitalize() only capitalizes the first letter of the entire string.
How would you handle spaces and punctuation in palindrome checking?
Remove spaces and punctuation using replace() or regular expressions, then check palindrome on the cleaned string.
Related Resources
- Strings in Python
- String Methods Tutorial
- String Analysis Program
- String Module Operations
- Python Strings MCQs
String Tips
- • Use [::-1] for quick string reversal
- • String methods don't modify original string
- • Consider case sensitivity in comparisons
- • Handle spaces and punctuation carefully