Hi,
i need your expertise in this one. i am new to python, i want to write an algorithm that takes a string as input and determines whether or not it is a palindrome.

please point me to the right direction.

Thanks in advance.
sabiut

If your string is just a single word like "racecar", then you can simply compare the string with it's reverse spelled counterpart.

If your string is a complete sentence like "A man, a plan, a canal, Panama!", then you should turn all characters to lower case, remove the punctuation marks and all whitespace, and finally compare the resulting string with it's reverse spelled counterpart.

A simple way to reverse spell is to use slicing ...

word = "banana"
word_rev = word[::-1]
print(word)      # banana
print(word_rev)  # ananab

Hi,
i need your expertise in this one. i am new to python, i want to write an algorithm that takes a string as input and determines whether or not it is a palindrome.

please point me to the right direction.

Thanks in advance.
sabiut

Hi
Thanks for your prompt reply i have written the code as below. i don't know what i did wrong, but it keeps giving me the"The word that you have enter is not palindrome"

can you please help me figure out what i did wrong?

Thanks,


>>> word=str(raw_input("Please enter a string:"))
Please enter a string:banana
>>> string ="word"
>>> word_rev =word[::-1]
>>> if(string == word_rev):
print"The word your have enter is a palindrome"
else:
print"The word you have enter is not a palindrome"


The word you have enter is not a palindrome

Add some print statements to ferret out what's happening, for example:
And Idle (comes with Python) is much easier to use than the Python shell/command line.

word=str(raw_input("Please enter a string:"))
##   Don't use 'string' as a variable name
input_string ="word"
word_rev =word[::-1]

print "comparing", input_string, word
if (input_string == word_rev):
    print "The word your have entered is a palindrome"
else:
   print"The word you have entered is not a palindrome"

And note that "banana" is not a palindrome, so you should use another word like "racecar".

Add some print statements to ferret out what's happening, for example:
And Idle (comes with Python) is much easier to use than the Python shell/command line.

word=str(raw_input("Please enter a string:"))
##   Don't use 'string' as a variable name
input_string ="word"
word_rev =word[::-1]

print "comparing", input_string, word
if (input_string == word_rev):
    print "The word your have entered is a palindrome"
else:
   print"The word you have entered is not a palindrome"

And note that "banana" is not a palindrome, so you should use another word like "racecar".

Hi,

I have rewrite the code, however when i enter the word "racecar" as stated. i am still getting "The word you have entered is not a palindrome. i think there must be something that i am missing here. can someone please advise help me with this. the code is as below.

Thanks in advance.

>>> word = str(raw_input("Please enter a string:"))
Please enter a string:racecar
>>> input_word = "word"
>>> word_rev = word[::-1]
>>> print "comparing",input_word,word
comparing word racecar
>>> if(input_word == word_rev):
print "The word you have enterd is a palindrome"
else:
print"The word you have entered is not a palindrome"


The word you have entered is not a palindrome

IDLE 2.6.4      
>>> word = raw_input("Please enter a string:")
Please enter a string:racecar
>>> # raw_input is returing a string so dont use "str"
>>> input_word = "word"
>>> input_word
'word'
>>> #Do you see the error now.
>>> word_rev = word[::-1]
>>> word_rev
'racecar'
>>> if input_word == word_rev:
	print "The word you have enterd is a palindrome"
else:
	print"The word you have entered is not a palindrome"

	
The word you have entered is not a palindrome
#Off course because you are comparing against "input_word" that is "word" not "racecar"

>>> #make it easy
>>> word = raw_input("Please enter a string:")
Please enter a string:racecar
>>> if word == word[::-1]:
	print "The word you have enterd is a palindrome"
else:
	print"The word you have entered is not a palindrome"

	
The word you have enterd is a palindrome
>>>

A function for this could look like this.

def is_palindrome(my_string):    
    return my_string.lower() == my_string[::-1].lower()
IDLE 2.6.4      
>>> word = raw_input("Please enter a string:")
Please enter a string:racecar
>>> # raw_input is returing a string so dont use "str"
>>> input_word = "word"
>>> input_word
'word'
>>> #Do you see the error now.
>>> word_rev = word[::-1]
>>> word_rev
'racecar'
>>> if input_word == word_rev:
	print "The word you have enterd is a palindrome"
else:
	print"The word you have entered is not a palindrome"

	
The word you have entered is not a palindrome
#Off course because you are comparing against "input_word" that is "word" not "racecar"

>>> #make it easy
>>> word = raw_input("Please enter a string:")
Please enter a string:racecar
>>> if word == word[::-1]:
	print "The word you have enterd is a palindrome"
else:
	print"The word you have entered is not a palindrome"

	
The word you have enterd is a palindrome
>>>

A function for this could look like this.

def is_palindrome(my_string):    
    return my_string.lower() == my_string[::-1].lower()

Thanks very much for your help. You a are genius.
i also want to sort a list of integers into increasing order but i am having difficulties figuring how the function should look like. i have start with the code as below. can someone please show me how the function of the code should look like.

Thanks in advance

>>> a=[4,2,6,0,7,3,9,21,15,12,17,1,0]
>>> a.sort()
>>> a
[0, 0, 1, 2, 3, 4, 6, 7, 9, 12, 15, 17, 21]
>>>

can someone please show me how the function of the code should look like.

Thanks in advance

>>> a=[4,2,6,0,7,3,9,21,15,12,17,1,0]
>>> a.sort()
>>> a
[0, 0, 1, 2, 3, 4, 6, 7, 9, 12, 15, 17, 21]
>>>

def list_sort(input_list,):
    '''Sort a list'''    
    return sorted(input_list)
    
def remove_dup(input_list):
    '''Remove duplicate from a list'''
    return set(input_list)

def main():
    '''This is called a doc string'''
    my_list = [5,4,2,8,4]
    
    #First we se what the functions dos
    print list_sort(my_list)
    print remove_dup(my_list)
    print '-'*20     
    
    #Then we can combine them,first remove duplicate then sort    
    remove_duplicate = remove_dup(my_list)
    print list_sort(remove_duplicate)   
    
if __name__ == '__main__':
    main()

'''-->Output
[2, 4, 4, 5, 8]
set([8, 2, 4, 5])
--------------------
[2, 4, 5, 8]
'''

You can look at this.
The code is pretty clear,but just a short explanation because you new to this.

Both function take one argument .
The argument in this case is my_list
When return is done function is finish.
You can change return with print,and call it like this list_sort(my_list) .
Then you just print out result in the function,nothing get returned out.

You see the avantage when using return,you can have many function return jobb you want to do.
And in the main() you can pick upp all data and print it out.

If you want function instead of changing the order of sequence itself use sorted function. If you want in opposite order you can give reversed=True parameter:

>>> a=[4,2,6,0,7,3,9,21,15,12,17,1,0]
>>> print sorted(a)
[0, 0, 1, 2, 3, 4, 6, 7, 9, 12, 15, 17, 21]
>>> print a
[4, 2, 6, 0, 7, 3, 9, 21, 15, 12, 17, 1, 0]
>>> print sorted(a,reverse=True)
[21, 17, 15, 12, 9, 7, 6, 4, 3, 2, 1, 0, 0]
>>>

If you want to take out duplicates you can make list set and turn it back to list

>>> b=list(set(a))
>>> b
[0, 1, 2, 3, 4, 6, 7, 9, 12, 15, 17, 21]
>>>

For removing letters I recommend translate method with None as translation table for python 2.6

>>> c="A man, a plan, a canal, Panama!"
>>> punct=',!?()/&%#-;.\':'
>>> c.lower().translate(None,punct)
'a man a plan a canal panama'
>>> punctwhite=',!?()/&%#-;.\': \t\n'
>>> c.lower().translate(None,punctwhite)
'amanaplanacanalpanama'
>>> d=c.lower().translate(None,punctwhite)
>>> d==d[::-1]
True
>>>
def list_sort(input_list,):
    '''Sort a list'''    
    return sorted(input_list)
    
def remove_dup(input_list):
    '''Remove duplicate from a list'''
    return set(input_list)

def main():
    '''This is called a doc string'''
    my_list = [5,4,2,8,4]
    
    #First we se what the functions dos
    print list_sort(my_list)
    print remove_dup(my_list)
    print '-'*20     
    
    #Then we can combine them,first remove duplicate then sort    
    remove_duplicate = remove_dup(my_list)
    print list_sort(remove_duplicate)   
    
if __name__ == '__main__':
    main()

'''-->Output
[2, 4, 4, 5, 8]
set([8, 2, 4, 5])
--------------------
[2, 4, 5, 8]
'''

You can look at this.
The code is pretty clear,but just a short explanation because you new to this.

Both function take one argument .
The argument in this case is my_list
When return is done function is finish.
You can change return with print,and call it like this list_sort(my_list) .
Then you just print out result in the function,nothing get returned out.

You see the avantage when using return,you can have many function return jobb you want to do.
And in the main() you can pick upp all data and print it out.

Thanks for responding, how to i test this

def list_sort(input_list,):
'''Sort a list'''
return sorted(input_list)

Thanks for responding, how to i test this

def list_sort(input_list,):
'''Sort a list'''
return sorted(input_list)

What do you mean?,like this.

def list_sort(input_list,):
    '''Sort a list'''    
    return sorted(input_list)

my_list = [5,9,6,3,7,4,5]    
print list_sort(my_list)    #[3, 4, 5, 5, 6, 7, 9]

It shoul be clear from my code example.

Thanks very much. i really appreciate your help. That was very helpful.

sabiut

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.