Computer >> Máy Tính >  >> Lập trình >> Python

Chương trình Python để kiểm tra xem một chuỗi có phải là palindrome hay không

Với một chuỗi, nhiệm vụ của chúng ta là kiểm tra thời tiết chuỗi này có phải là palindrome hay không.

Thuật toán

Step1: Enter string as an input.
Step2: Using string slicing we reverse the string and compare it back to the original string.
Step3: Then display the result.

Mã mẫu

my_string=input("Enter string:")
if(my_string==my_string[::-1]):
   print("The string is a palindrome")
else:
   print("The string isn't a palindrome")

Đầu ra

Enter string:madam
The string is a palindrome
Enter string:python
The string isn't a palindrome