Python程序檢查字符串是否是回文

What is palindrome string?

什么是回文字符串?

A string is a palindrome if the string read from left to right is equal to the string read from right to left i.e. if the actual string is equal to the reversed string.

如果從左至右讀取的字符串等于從右至左讀取的字符串,即實際字符串等于反向字符串,則該字符串為回文

In the below program, we are implementing a python program to check whether a string is a palindrome or not?

在下面的程序中,我們正在實現一個python程序來檢查字符串是否是回文?

Steps:

腳步:

  • First, find the reverse string

    首先,找到反向字符串

  • Compare whether revers string is equal to the actual string

    比較反轉字符串是否等于實際字符串

  • If both are the same, then the string is a palindrome, otherwise, the string is not a palindrome.

    如果兩者相同,則該字符串是回文,否則,該字符串不是回文。

Example:

例:

    Input: 
"Google"
Output:
"Google" is not a palindrome string
Input:
"RADAR"
Output:
"RADAR" is a palindrome string

Method 1: Manual

方法1:手動

# Python program to check if a string is 
# palindrome or not
# function to check palindrome string
def isPalindrome(string):
result = True
str_len = len(string)
half_len= int(str_len/2)
for i in range(0, half_len):
# you need to check only half of the string
if string[i] != string[str_len-i-1]:
result = False
break
return result 
# Main code
x = "Google"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")  
x = "ABCDCBA"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")
x = "RADAR"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string") 

Output

輸出量

Google is not a palindrome string
ABCDCBA is a palindrome string
RADAR is a palindrome string

Method 2: Slicing

方法2:切片

# Python program to check if a string is 
# palindrome or not
# function to check palindrome string
def isPalindrome(string):
rev_string = string[::-1]
return string == rev_string
# Main code
x = "Google"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")  
x = "ABCDCBA"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")
x = "RADAR"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")  

Output

輸出量

Google is not a palindrome string
ABCDCBA is a palindrome string
RADAR is a palindrome string

翻譯自: https://www.includehelp.com/python/program-to-check-if-a-string-is-palindrome-or-not.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542307.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542307.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542307.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

content屬性的4種用途

content屬性瀏覽器支持情況&#xff0c;兼容到IE8瀏覽器&#xff0c;IE7及以下不支持用途一、配合:before及:after偽元素插入文本<div><p>偽元素</p> </div>p:before{content:CSS3;color:#4bb;font-weight:bold;margin-right:20px;background:#f0f0f0;…

內蒙古師范大學計算機科學技術學院,內蒙古師范大學計算機科學技術碩士生導師——李成城...

李成城&#xff0c;教授&#xff0c;碩導&#xff0c;1971年7月出生于內蒙古呼倫貝爾盟。2002年9月-2005年7月在北京郵電大學信息工程學院學習&#xff0c;獲得工學博士學位&#xff0c;主要研究領域是&#xff1a;自然語言理解、機器學習、圖像識別。1993年在內蒙古師范大學計…

超清世界地圖可放大_3D高清世界地圖

查看世界地圖的工具有哪些可以提供呢&#xff1f;3D世界地圖官方版是簡單好用的世界地圖工具&#xff0c;可以看到地球的大概模樣&#xff0c;選擇不一樣的海洋能看到不一樣的事物&#xff0c;這也是一款3d世界地圖高清地圖&#xff0c;需要用地圖軟件的可以下載。3D世界地圖官…

Java ObjectInputStream readUnshared()方法與示例

ObjectInputStream類readUnshared()方法 (ObjectInputStream Class readUnshared() method) readUnshared() method is available in java.io package. readUnshared()方法在java.io包中可用。 readUnshared() method is used to read "non-shared" or "unshare…

許多計算機英語,計算機英語翻譯

1數據是未經組織的事實的集合,數據可以包括單詞,數字,圖像和聲音.2計算機由許多硬件部件構成,這些硬件與軟件一起工作,以便執行計算,組織數據及與其他計算機通信的任務,3硬件部件包括輸入設備,輸出設備,系統單元 ,存儲設備和通信設備.4輸入設備讓用戶向計算機存儲器輸入數據和命…

工欲善其事必先利其器(一)

2019獨角獸企業重金招聘Python工程師標準>>> 寫在前面的話&#xff1a;紙上得來終學淺&#xff0c;絕知此事要躬行。還是自己敲一遍記得牢。 下載和安裝 Emmet為大部分流行的編輯器都提供了安裝插件&#xff0c;下面是它們的下載鏈接&#xff1a; Sublime Text Ecli…

windows 2008r2文件服務器部分用戶訪問不了_蘋果設備如何訪問 Windows 文件共享?...

前幾天寫了一篇關于 Mac 文件共享的。今天繼續聊聊 Mac、iPad 和 iPhone 如何訪問 Windows 的文件共享。Windows 開啟文件共享Windows 搜索并打開「高級共享設置」。在「專用」勾選「啟用網絡發現」和「啟用文件和打印機共享」。然后選擇一個 Windows 上想要共享的位置&#xf…

Java文件類boolean isDirectory()方法(帶示例)

文件類boolean isDirectory() (File Class boolean isDirectory()) This method is available in package java.io.File.isDirectory(). 軟件包java.io.File.isDirectory()中提供了此方法。 This method is used to check whether the file is specified by filepath is a dire…

微信小程序簡單入門1

參考文檔&#xff1a;https://mp.weixin.qq.com/debug/wxadoc/dev/index.html1 創建項目開發者工具安裝完成后&#xff0c;打開并使用微信掃碼登錄。選擇創建“項目”&#xff0c;填入上文獲取到的 AppID &#xff0c;&#xff08;無appid直接選擇&#xff09;設置一個本地項目…

leetcode數組匯總_LeetCode刷題實戰118:楊輝三角

算法的重要性&#xff0c;我就不多說了吧&#xff0c;想去大廠&#xff0c;就必須要經過基礎知識和業務邏輯面試算法面試。所以&#xff0c;為了提高大家的算法能力&#xff0c;這個公眾號后續每天帶大家做一道算法題&#xff0c;題目就從LeetCode上面選 &#xff01;今天和大家…

位運算使奇數+1 偶數-1_C ++程序打印從1到N的所有偶數和奇數

位運算使奇數1 偶數-1Problem: Take input from the user (N) and print all EVEN and ODD numbers between 1 to N. 問題&#xff1a;從用戶那里輸入(N)&#xff0c;并打印1至N之間的所有偶數和奇數編號。 Solution: 解&#xff1a; Input an integer number (N). 輸入一個整…

javascript 模塊化機制

1. 概述 js發展初期暴露了其缺陷&#xff1a;缺乏模塊&#xff0c;后來提出了commonJS規范來規范其模塊的規范。作為JavaScript新手&#xff0c;發現對于其JavaScript的模塊機制&#xff0c;不是很理解。我查閱了一些資料整理了JavaScript CommonJS的原理和機制。 2. JavaScrip…

c語言 宏定義 去除宏定義_如何檢查是否在C中定義了宏?

c語言 宏定義 去除宏定義To check whether a Macro is defined or not in C language – we use #ifdef preprocessor directive, it is used to check Macros only. 要檢查是否用C語言定義了宏 -我們使用#ifdef預處理程序指令&#xff0c;它僅用于檢查宏。 Syntax: 句法&…

多線程下不能用truncate嗎_那么多的化妝品,懷孕后都不能用了嗎?

前幾天圓夢參加了青島的美博會&#xff0c;里面的化妝品真多啊&#xff0c;無論是護膚、美白、彩妝比比皆是&#xff0c;看的人眼&#xff08;liu&#xff09;花&#xff08;lian&#xff09;繚&#xff08;wang&#xff09;亂&#xff08;fan&#xff09;。雖說國務院宣布的新…

手機html5性能測試工具,HTML5模塊?性能方面8大測試環節_小米 M3_手機硬件頻道-中關村在線...

Vellamo作為一款專攻網頁瀏覽性能測試的工具&#xff0c;在這方面的測試環節也相對復雜很多。這一部分在Vellamo的HTML5測試環節中通過14項測試來進行體現&#xff0c;而接下來我們會對著實際個測試項進行相應的介紹。See The Sun Canvas/Pixel Blender測試界面See The Sun Can…

[計算機網絡]httpserver--如何解析HTTP請求報文

這個http server的實現源代碼我放在了我的github上&#xff0c;有興趣的話可以點擊查看哦。 在上一篇文章中&#xff0c;講述了如何編寫一個最簡單的server&#xff0c;但該程序只是接受到請求之后馬上返回響應&#xff0c;實在不能更簡單。在正常的開發中&#xff0c;應該根據…

python字典副本_如何復制字典并僅在Python中編輯副本?

python字典副本Python never implicitly copies the dictionary or any objects. So, while we set dict2 dict1, were making them refer to the same dictionary object. Hence, even when we mutate the dictionary, all the references made to it, keep referring to the…

英特爾核芯顯卡控制面板沒有了_核顯和獨顯、集成顯卡有什么區別

集成顯卡&#xff1a;一般不帶有顯存&#xff0c;而是使用系統的一部分主內bai存作為顯存&#xff0c;具體的數量一般是系統根據需要自動動態調整的。顯然&#xff0c;如果使用集成顯卡運行需要大量占用內存的空間&#xff0c;對整個系統的影響會比較明顯&#xff0c;此外系統內…

徐州初中計算機學校排名2015,徐州初中學校排名,徐州重點初中排名詳細榜單

2018年徐州初中學校排名,徐州重點初中排名詳細榜單孩子小升初&#xff0c;幾乎所有的家長都會陷入糾結&#xff0c;都想為孩子選擇一所“好學校”&#xff0c;在擇校過程中&#xff0c;家長們總想知道徐州初中學校排名以及徐州重點初中排名詳細榜單&#xff0c;但這里小編提醒一…

分布式計算 MapReduce與yarn工作機制

一、第一代hadoop組成與結構第一代Hadoop&#xff0c;由分布式存儲系統HDFS和分布式計算框架MapReduce組成&#xff0c;其中&#xff0c;HDFS由一個NameNode和多個DataNode組成&#xff0c;MapReduce由一個JobTracker和多個TaskTracker組成&#xff0c;對應Hadoop版本為Hadoop …