判断字符串是否为回文 python

回文正序和逆序一样的字符串,例如abccba

方法一

def is_palindrome1(text):
    l = list(text)
    l.reverse()
    t1 = ‘‘.join(l)
    if t1 == text:
        print ‘the text is palindrome‘
    else:
        print ‘the text is not palindrome‘

方法二

def is_palindrome2(text):
    t1 = text[::-1]
    if t1 == text:
        print ‘the text is palindrome‘
    else:
        print ‘the text is not palindrome‘

方法三

def is_palindrome3(text):
    r = True
    for x in range(len(text)):
        print x,text[x]
        if text[x] != text[len(text)-x-1]:
            print 1
            r = False
            break
    if r == True:
        print ‘the text is palindrome‘
    else:
        print ‘the text is not palindrome‘

原文地址:https://www.cnblogs.com/xiaomingtx/p/9099600.html

时间: 2024-08-01 02:54:14

判断字符串是否为回文 python的相关文章

用递归方法判断字符串是否是回文(Recursion Palindrome Python)

所谓回文字符串,就是一个字符串从左到右读和从右到左读是完全一样的.比如:"level" ."aaabbaaa". "madam"."radar". 如何判断字符串是否是回文呢?解决思路如下: 1. 采取穷举法(Brute Force algorithm),枚举并检查(enumerate & check)字串符的第一项和最后一项是否等同 2. 把检查范围逐步缩小,如果字串符的第一项和最后一项等同,那么去除字串符的第一项和

AC日记——判断字符串是否为回文 openjudge 1.7 33

33:判断字符串是否为回文 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个字符串,输出该字符串是否回文.回文是指顺读和倒读都一样的字符串. 输入 输入为一行字符串(字符串中没有空白字符,字符串长度不超过100). 输出 如果字符串是回文,输出yes:否则,输出no. 样例输入 abcdedcba 样例输出 yes 思路: 模拟: 来,上代码: #include<cstdio> #include<string> #include<cstring>

Java 用递归判断字符串是否可以回文

设计思想: 判断字符串是否可以回文,首先字符串长度为0,或只有一个字符,即字符串长度为1是可以回文的:再然后字符串长度大于1,让第一个字符和最后一个比较,若相等,则用递归,调用函数比较第二个和倒数第二个,以此类推. 源代码: package test2; import java.util.Scanner; public class jiecheng { public static void main(String[] args) { // TODO 自动生成的方法存根 System.out.pr

Java - 判断字符串是否是回文

首先,回文是指类似于“12345”,“abcdcba”的形式,即正念和反念都是一样的字符串 判断字符串是否是回文,这边介绍3种办法 将字符串翻转,判断翻转后的字符串和原字符串是否相等 1 public static void main(String[] args) { 2 String s="abcdcba"; 3 // 用StringBuilder的reverse方法将字符串反转 4 StringBuilder sb=new StringBuilder(s); 5 String af

Valid Palindrome ——判断字符串是否为回文串

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41488377 Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama&

C++刷题——2802: 判断字符串是否为回文

Description 编写程序,判断输入的一个字符串是否为回文.若是则输出"Yes",否则输出"No".所谓回文是指順读和倒读都是一样的字符串. Input Output Sample Input abcddcba Sample Output Yes /* Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作者:陈丹妮 * 完成日期:2015年 6 月 1 日 * 版 本

递归判断字符串是否为回文

源代码 package test; import java.util.Scanner; public class Palindrome { public static void main(String[] args) {  System.out.println("请输入判断的字符串");  Scanner scan=new Scanner(System.in);  String str=scan.next();  int n = str.length();  boolean flag=

用递归方式判断字符串是否是回文

题目要求:使用递归方式判断某个字串是否是回文( palindrome )回文”是指正着读.反着读都一样的句子.比如“我是谁是我” package zzm; import java.util.Scanner; public class Hw { static Scanner input=new Scanner(System.in); public static void main(String[] args) { System.out.print("请输入一串字符:"); String

用递归判断字符串是否为回文

程序基本思路: 1.首先判断若字符串的长度为0或1,如果是,则这个字符串是回文序列,程序直接结束 2.若字符串长度大于1,先将字符串转化成字符数组,定义旗帜flag.字符数组头和尾标识,将字符数组首地址和以上定义传给递归方法 3.用头尾标记数组头尾字符来进行一一比较,如果一致,继续递归调用自身,直至达到递归终止条件.如果中途有不匹配的,返回输出字符串不是回文序列. 递归终止条件为头尾标记相等或相差为1,返回输出字符串是回文序列. import java.util.Scanner; public