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:

  1. Start
  2. Input a string from the user
  3. Create a reversed version of the string using slicing [::-1]
  4. Compare the original string with the reversed string
  5. If they are equal, the string is a palindrome
  6. Otherwise, it is not a palindrome
  7. Display the result
  8. Stop

For Case Conversion:

  1. Input a string from the user
  2. Use built-in string methods for case conversion
  3. 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:

  1. Understanding string slicing with [::-1] for reversing strings
  2. String comparison for palindrome detection
  3. Using built-in string methods for case conversion
  4. Difference between capitalize(), title(), and other case methods

This program demonstrates fundamental string manipulation techniques that are essential for text processing applications.

VIVA QUESTIONS

  1. What is a palindrome string?

    A palindrome string is a string that reads the same forwards and backwards. Examples include "madam", "racecar", "level", etc.

  2. 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.

  3. 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.

  4. 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()

  5. What does swapcase() method do?

    swapcase() converts uppercase characters to lowercase and lowercase characters to uppercase in the string.

  6. 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.

  7. 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.

  8. 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

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

Practice More

Try more string manipulation programs to master text processing.

View All Programs →