博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python程序检查字符串是否是回文
阅读量:2528 次
发布时间:2019-05-11

本文共 2282 字,大约阅读时间需要 7 分钟。

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 stringdef 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 codex = "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 stringABCDCBA is a palindrome stringRADAR is a palindrome string

Method 2: Slicing

方法2:切片

# Python program to check if a string is # palindrome or not# function to check palindrome stringdef isPalindrome(string):  rev_string = string[::-1]  return string == rev_string# Main codex = "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 stringABCDCBA is a palindrome stringRADAR is a palindrome string

翻译自:

转载地址:http://qdtzd.baihongyu.com/

你可能感兴趣的文章
《机器学习实战》学习笔记第二章 —— K-近邻算法
查看>>
uni-app 引入本地iconfont的正确姿势以及阿里图标引入
查看>>
DSB
查看>>
Java中的阻塞队列
查看>>
前端软件sublime的一些常用快捷键
查看>>
openssl 升级
查看>>
2017.10.30 天晴 昨天十公里没减肥
查看>>
Git 打标签(分布式版本控制系统)
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
HttpClient读取数据乱码的解决方案
查看>>
如何使用FireBug插件查询元素的xPath属性
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>