python檢測字串是否是回文字串

2021-08-15 02:59:50 字數 1074 閱讀 8146

# -*- coding: utf-8 -*-

"""spyder editor

author:linguiyuan

"""def reverse(text):

return text[::-1]

def ishuiwen(text):

return text == reverse(text)

text = input("輸入乙個字串判斷是否是回文字串:\n")

if ishuiwen(text):

print("該字串是回文")

else:

print("該字串不是回文")

注:這裡我們使用切片來實現字串的迴轉

#改進版本

檢查文字是不是回文也應該忽略、空格和大小寫。例如,"rise to vote,sir." 也是回文,但當前的程式並不能指明。

# -*- coding: utf-8 -*-

"""spyder editor

author:linguiyuan

"""import string

def reverse(text):

return text[::-1]

def ishuiwen(text):

text = text.lower()

text = text.replace(' ','') #替換掉空格

#替換掉標點符號

for char_biaodian in string.punctuation:

text = text.replace(char_biaodian,'')

return text == reverse(text)

def main():

text = input("輸入乙個字串判斷是否是回文字串:\n")

if ishuiwen(text):

print("該字串是回文")

else:

print("該字串不是回文")

if __name__ == '__main__':

main()

JAVA檢測字串是否數值

一,開篇 對於 檢測字串是否數值 網上搜尋結果確實不少,基本思路都是使用正規表示式,基本上都是直接上 基本上好像都靠譜 但是談思路的不多。二,什麼樣的字串才是數值 總的來說,字串的字元只能存在於 正號 負號 小數點 0 9的數字,且 正號和負號只能出現在頭部且最多只能出現1次 小數點最多只能出現1次...

檢測字串

instanceof 用來檢測某乙個例項是否屬於這個類 constructor 利用原型構造器的方式檢測資料型別 object.prototype.tostring.call 借用內建類object原型上的tostring方法實現資料型別檢測console.log typeof typeof typ...

Python 遞迴 檢測字串是否為 回文

源 檢測字串是否是 回文字串 回文概念 從前往後念 與 從後往前念 都一樣。def hui wen str 基線條件 如果字串的長度小於 2 位,那麼改字串就為 回文字串 if len str 2 是 回文 return true 如果字串的 第一位 與 最後一位 不相等,那麼不為 回文字串 eli...