📌  相关文章
📜  用于检查字符串是否为回文的Python程序

📅  最后修改于: 2022-05-13 01:54:30.139000             🧑  作者: Mango

用于检查字符串是否为回文的Python程序

给定一个字符串,编写一个Python函数来检查它是否是回文。如果字符串的反转与字符串相同,则称该字符串为回文。例如,“radar”是回文,但“radix”不是回文。

例子:

Input : malayalam
Output : Yes

Input : geeks
Output : No

方法#1

  1. 查找字符串的反转
  2. 检查反向和原始是否相同。
Python
# function which return reverse of a string
 
def isPalindrome(s):
    return s == s[::-1]
 
 
# Driver code
s = "malayalam"
ans = isPalindrome(s)
 
if ans:
    print("Yes")
else:
    print("No")


Python
# function to check string is
# palindrome or not
def isPalindrome(str):
 
    # Run loop from 0 to len/2
    for i in range(0, int(len(str)/2)):
        if str[i] != str[len(str)-i-1]:
            return False
    return True
 
# main function
s = "malayalam"
ans = isPalindrome(s)
 
if (ans):
    print("Yes")
else:
    print("No")


Python
# function to check string is
# palindrome or not
def isPalindrome(s):
 
    # Using predefined function to
    # reverse to string print(s)
    rev = ''.join(reversed(s))
 
    # Checking if both string are
    # equal or not
    if (s == rev):
        return True
    return False
 
# main function
s = "malayalam"
ans = isPalindrome(s)
 
if (ans):
    print("Yes")
else:
    print("No")


Python
# Python program to check
# if a string is palindrome
# or not
 
x = "malayalam"
 
w = ""
for i in x:
    w = i + w
 
if (x == w):
    print("Yes")
else:
    print("No")


Python
# Python program to check
# if a string is palindrome
# or not
st = 'malayalam'
j = -1
flag = 0
for i in st:
    if i != st[j]:
        flag = 1
        break
    j = j - 1
if flag == 1:
    print("NO")
else:
    print("Yes")


Python3
# Recursive function to check if a
# string is palindrome
def isPalindrome(s):
 
    # to change it the string is similar case
    s = s.lower()
    # length of s
    l = len(s)
 
    # if length is less than 2
    if l < 2:
        return True
 
    # If s[0] and s[l-1] are equal
    elif s[0] == s[l - 1]:
 
        # Call is palindrome form substring(1,l-1)
        return isPalindrome(s[1: l - 1])
 
    else:
        return False
 
# Driver Code
s = "MalaYaLam"
ans = isPalindrome(s)
 
if ans:
    print("Yes")
 
else:
    print("No")


输出 :

Yes

迭代方法:此方法由Shariq Raza提供。从 start 到 length/2 运行一个循环,并检查字符串的第一个字符到最后一个字符以及倒数第二个字符,依此类推……。如果任何字符不匹配,则该字符串将不是回文。

以下是上述方法的实现:

Python

# function to check string is
# palindrome or not
def isPalindrome(str):
 
    # Run loop from 0 to len/2
    for i in range(0, int(len(str)/2)):
        if str[i] != str[len(str)-i-1]:
            return False
    return True
 
# main function
s = "malayalam"
ans = isPalindrome(s)
 
if (ans):
    print("Yes")
else:
    print("No")

输出:

Yes

使用内置函数反转字符串的方法:此方法由Shariq Raza提供。在此方法中,预定义函数' '.join(reversed(字符串))用于反转字符串。

下面是上述方法的实现:

Python

# function to check string is
# palindrome or not
def isPalindrome(s):
 
    # Using predefined function to
    # reverse to string print(s)
    rev = ''.join(reversed(s))
 
    # Checking if both string are
    # equal or not
    if (s == rev):
        return True
    return False
 
# main function
s = "malayalam"
ans = isPalindrome(s)
 
if (ans):
    print("Yes")
else:
    print("No")

输出:

Yes

使用一个额外变量的方法:在这种方法中,用户将字符串中的一个字符一个接一个地存储在一个空变量中。存储完所有字符后,用户将比较两个字符串并检查它是否是回文。

Python

# Python program to check
# if a string is palindrome
# or not
 
x = "malayalam"
 
w = ""
for i in x:
    w = i + w
 
if (x == w):
    print("Yes")
else:
    print("No")

输出:

Yes

使用标志的方法:在这种方法中,用户在一个for循环中比较从开始和结束的每个字符,如果字符不匹配,那么它将改变标志的状态。然后它将检查标志的状态并相应地打印它是否是回文。

Python

# Python program to check
# if a string is palindrome
# or not
st = 'malayalam'
j = -1
flag = 0
for i in st:
    if i != st[j]:
        flag = 1
        break
    j = j - 1
if flag == 1:
    print("NO")
else:
    print("Yes")

输出:

Yes

使用递归的方法

此方法比较字符串的第一个和最后一个元素,并将子字符串的其余部分提供给对其自身的递归调用。

Python3

# Recursive function to check if a
# string is palindrome
def isPalindrome(s):
 
    # to change it the string is similar case
    s = s.lower()
    # length of s
    l = len(s)
 
    # if length is less than 2
    if l < 2:
        return True
 
    # If s[0] and s[l-1] are equal
    elif s[0] == s[l - 1]:
 
        # Call is palindrome form substring(1,l-1)
        return isPalindrome(s[1: l - 1])
 
    else:
        return False
 
# Driver Code
s = "MalaYaLam"
ans = isPalindrome(s)
 
if ans:
    print("Yes")
 
else:
    print("No")

输出:

Yes