Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . five Python programs to check whether a string is a palindrome or not

five Python programs to check whether a string is a palindrome or not

Become More Then A coder | Learn & Start Coding Now.

five Python programs to check whether a string is a palindrome or not:


### 1. Using Slicing:

```python
# Program 1: Checking if a string is a palindrome using slicing
def is_palindrome(s):
    return s == s[::-1]

string = "radar"
print(f"{string} is a palindrome:", is_palindrome(string))
```

### 2. Using Iterative Approach:

```python
# Program 2: Checking if a string is a palindrome using iterative approach
def is_palindrome(s):
    length = len(s)
    for i in range(length // 2):
        if s[i] != s[length - i - 1]:
            return False
    return True

string = "level"
print(f"{string} is a palindrome:", is_palindrome(string))
```

### 3. Using Recursion:

```python
# Program 3: Checking if a string is a palindrome using recursion
def is_palindrome(s):
    if len(s) < 2:
        return True
    if s[0] != s[-1]:
        return False
    return is_palindrome(s[1:-1])

string = "noon"
print(f"{string} is a palindrome:", is_palindrome(string))
```

### 4. Using List Reversal:

```python
# Program 4: Checking if a string is a palindrome using list reversal
def is_palindrome(s):
    reversed_string = ''.join(reversed(s))
    return s == reversed_string

string = "madam"
print(f"{string} is a palindrome:", is_palindrome(string))
```

### 5. Using Deque:

```python
# Program 5: Checking if a string is a palindrome using deque
from collections import deque

def is_palindrome(s):
    deque_string = deque(s)
    while len(deque_string) > 1:
        if deque_string.popleft() != deque_string.pop():
            return False
    return True

string = "racecar"
print(f"{string} is a palindrome:", is_palindrome(string))
```

Each of these programs checks whether the given string is a palindrome or not using different approaches - slicing, iterative approach, recursion, list reversal, and deque. Feel free to replace the `string` variable with any desired string to check for palindrome.

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.