- #1
- 7,378
- 11,342
- TL;DR Summary
- I have code , a function Rev(x) I wrote, to determine the reverse of a string . I want to use it to determine whether a string is a palindrome, test whether String =Rev(String) . I also want to write another Palindrome tester to practice slicing, which would loop to test whether for a string of length k, String[i]= String{k-i]; i=1,2,..,k
Using Jupyter on Win 10
Hi,
I'm trying to write a program in Python 3 to determine whether a string is a palindrome.
I wrote one already ; it runs, to reverse a string.
I now want to use it to determine palindromes , by testing whether a string equals its reverse.
To practice slicing as well, I want to write the palindrome tester to loop to determine, for a string of length k, whether the
ith spot; i=1,2,..,k equals the (k-i)th spot .
Here's what I have :
1) To reverse
It works. I'm trying to use it to test for Palindromic strings , using the test whether String=Rev(String):
The code below would be written below the above code
But it's not running . How can I combine the two definitions above it to determine Palindromes?
Message:
Another option is using just the slicing approach as a stand-alone:
Error message is :
"File "<ipython-input-30-6b6272cc2aad>", line 10
else:
^
SyntaxError: invalid character in identifier " Any suggestions?
I'm trying to write a program in Python 3 to determine whether a string is a palindrome.
I wrote one already ; it runs, to reverse a string.
I now want to use it to determine palindromes , by testing whether a string equals its reverse.
To practice slicing as well, I want to write the palindrome tester to loop to determine, for a string of length k, whether the
ith spot; i=1,2,..,k equals the (k-i)th spot .
Here's what I have :
1) To reverse
Python:
# Reversing a string :
def Rev(x):
String = str(x)
if String[0]= '- ' :
return int( '-' + String[ :0:-1} )
else:
return int( String[:: -1] )
It works. I'm trying to use it to test for Palindromic strings , using the test whether String=Rev(String):
The code below would be written below the above code
Python:
def pal(x):
String =st(x)
if String =Rev(String):
return('Yes, it's a Pal')
else :
return('not a Pal')
But it's not running . How can I combine the two definitions above it to determine Palindromes?
Message:
Another option is using just the slicing approach as a stand-alone:
Python:
def Pal(x):
String=Str(x)
j=len(String)
For i in range(1,j):
if String[i] = String[j-i]:
return('Yes , Pal')
else:
return('no, pal')
Error message is :
"File "<ipython-input-30-6b6272cc2aad>", line 10
else:
^
SyntaxError: invalid character in identifier " Any suggestions?
Last edited: