Palindrome Check RE

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 97:- Palindrome Check RE

To check if a string is a palindrome using regular expressions (RE) in Python, we can remove all non-alphanumeric characters from the string and then compare it with its reverse to determine if it reads the same forwards and backwards.

Here's a Python code snippet to check if a string is a palindrome using regular expressions:

pythonCopy code

import re def is_palindrome_with_re(input_string):    # Use a regular expression to remove all non-alphanumeric characters from the string    alphanumeric_string = re.sub(r'[^a-zA-Z0-9]', '', input_string)    # Convert the alphanumeric string to lowercase for case-insensitive comparison    alphanumeric_string = alphanumeric_string.lower()    # Check if the alphanumeric_string is equal to its reverse    return alphanumeric_string == alphanumeric_string[::-1] # Test the function input_string = "A man, a plan, a canal: Panama" result = is_palindrome_with_re(input_string) print(result)  # Output: True

In this example, the re.sub() function with the regular expression pattern [^a-zA-Z0-9] is used to remove all characters that are not alphanumeric from the input string. The lower() method is then used to convert the alphanumeric string to lowercase for case-insensitive comparison. Finally, the function checks if the alphanumeric string is equal to its reverse, and if they are the same, it returns True, indicating that the original input string is a palindrome.

Note that this approach is just one way to check for palindromes using regular expressions, and there are alternative methods without regular expressions as well. The regular expression used here is to remove non-alphanumeric characters and not directly used for checking palindrome property.

14. Week7 - Assignments

2 Comments

@mk.info.work
mk.info.work Feb 17, 2024 at 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

@sciaku1
sciaku1 Jan 11, 2024 at 3:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support