Monday, February 20, 2023
HomeSoftware EngineeringHow you can Verify if String is a Palindrome in Python

How you can Verify if String is a Palindrome in Python

The problem

Write a operate that checks if a given string (case insensitive) is a palindrome.

The answer in Python

Choice 1:

def is_palindrome(s):
    s = s.decrease()
    for i, merchandise in enumerate(s):
        if i<len(s)/2:
            if s[i]!=s[len(s)-i-1]:
                return False
    return True

Choice 2:

def is_palindrome(s):
    s = s.decrease()
    return s == s[::-1]

Choice 3:

def is_palindrome(s):
    return s.decrease()==s[::-1].decrease()

Take a look at instances to validate our answer

@check.describe('pattern checks')
def sample_tests():
    check.assert_equals(is_palindrome('a'), True)
    check.assert_equals(is_palindrome('aba'), True)
    check.assert_equals(is_palindrome('Abba'), True)
    check.assert_equals(is_palindrome('malam'), True)
    check.assert_equals(is_palindrome('walter'), False)
    check.assert_equals(is_palindrome('kodok'), True)
    check.assert_equals(is_palindrome('Kasue'), False)



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments