0

How to check if word is palindrome or not in python. Using for loop.

You cannot use negative indices You can use only 1 string variables

26th Sep 2019, 8:16 AM
Shusil
5 Respostas
+ 6
Shusil If you need help, you can post the code you're struggling with!  • SEARCH for similar QUESTIONS or ANSWERS before posting  • Include relevant TAGS  • https://www.sololearn.com/post/75089/?ref=app
26th Sep 2019, 10:18 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 3
Hint: Get the string length, decrement it by 1, that's the index of last character to check. Since you will be checking each character from both end of the string, you'll need half the string length as your loop range limit let it be <half>. Remember to divide with floor division operator to have it an integral value. Run a for loop <i> with <half> as range limit. During loop see if character on the i-th position NOT equal to the character on the <half> - <i> position. If so, immediately return False. If the loop completed then return True. P.S. Don't ask for code, you won't get it from me XD
26th Sep 2019, 9:59 AM
Ipang
+ 2
Can you show us your attempt first?
26th Sep 2019, 9:57 AM
Russ
Russ - avatar
0
def pal(s): for i in range(0): pass return s == s[::-1]
26th Sep 2019, 12:13 PM
Thoq!
Thoq! - avatar
0
I think while loop work better.Here is an example https://code.sololearn.com/c18Ltdc8It25/?ref=app
22nd Feb 2021, 9:19 AM
Sonam Tashi