Python 程式檢查一個字串是否迴文


給定一個字串,我們的任務就是檢查這個字串是否迴文。

演算法

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.

示例程式碼

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")

輸出

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

更新於:2020-06-26

3000+ 瀏覽

啟動您的 職業生涯

完成課程獲得認證

立即開始
廣告