Lecture

Checking for Palindrome Strings - Problem Solving

Check out 3 ways to determine if a string reads the same backwards.


Method 1
def solution(s): return s == s[::-1] # Reverse the string and compare it with the original to check for palindrome

When three values are provided for slicing using :, it represents [start:end:step].

start is the starting index, end is the ending index, and step indicates the increment.

start and end can be omitted, meaning the entire string from start to finish is considered.

[::-1] is a common Python expression to reverse a string. Here, :: means the entire string and -1 indicates that it should be read in reverse order.


Example Usage

Input/Output Example
result1 = solution("radar") print(result1) # Output: True result2 = solution("hello") print(result2) # Output: False

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help