Sunday, 12 January 2025

Day 87: Python Program to Check if a Given String is Palindrome


 def is_palindrome(s):

    s = s.replace(" ", "").lower()

        return s == s[::-1]

input_string = input("Enter a string: ")

if is_palindrome(input_string):

    print(f'"{input_string}" is a palindrome.')

else:

    print(f'"{input_string}" is not a palindrome.')

#source code --> clcoding.com 

Code Explanation:

def is_palindrome(s):
This line defines a function called is_palindrome that takes one argument, s.
The argument s is expected to be the string that we want to check if it is a palindrome.

    s = s.replace(" ", "").lower()
This line modifies the input string s to prepare it for the palindrome check:
s.replace(" ", ""):
Removes all spaces from the string.
.lower():
Converts all characters in the string to lowercase.
These transformations ensure the check ignores spaces and capitalization, making it case-insensitive.
    return s == s[::-1]
This line checks if the modified string is the same as its reverse:
s[::-1]:
This is Python slicing syntax that creates a reversed version of the string.

s == s[::-1]:
Compares the string s with its reversed version.
If they are the same, it means the string is a palindrome, and the function returns True. Otherwise, it returns False.
input_string = input("Enter a string: ")
This line asks the user to enter a string and stores the input in the variable input_string.
The input() function allows the user to type a string when the program runs.

if is_palindrome(input_string):
This calls the is_palindrome function, passing the user's input (input_string) as the argument.
If the function returns True (indicating the string is a palindrome), the if block will execute.
If it returns False, the else block will execute.

    print(f'"{input_string}" is a palindrome.')
If the function determines that the string is a palindrome, this line prints a message confirming that.
The f before the string allows you to include variables inside curly braces {} in the printed message.

else:
    print(f'"{input_string}" is not a palindrome.')
If the function determines that the string is not a palindrome, this line prints a message saying so.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (84) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (953) Python Coding Challenge (398) Python Quiz (53) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses