How to Check Palindrome String & Number in Python – GATE, Interview & Placement Practice
15K views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you two
0:04
very common programs which is asked
0:06
inside Python
0:08
uh if you go for interview for
0:10
programming or competitive exams such as
0:13
GATE so for placements which is for
0:18
program for palendrome palindrome is
0:20
actually a sequence in which I will show
0:25
you both for strings and numbers
0:28
palendrome is defined as let's suppose I
0:30
take first of all the example of a
0:32
string this is a simple Python program I
0:35
have written whether to detect whether a
0:37
string is palendrome or
0:40
not so if I just execute this program
0:43
here Python app py and it will ask you
0:46
to enter a
0:48
string so it will actually ask you to
0:51
enter a string so if I write here
0:54
t so now what happens if you write TAT
0:58
in the uh same manner T
1:02
A or if you reverse
1:05
it if you just write in the reverse
1:08
order as well it sounds same T A T and T
1:13
A T so you will see this string is a
1:16
palindrome so the sequence remains the
1:20
same here so if I go from this location
1:22
TAT or this location from left to right
1:25
or right to left the sequence remains
1:27
the same so this is actually a
1:29
palendrome string and the example
1:33
suppose I take another example this
1:35
string is not a
1:38
palindrome from uh left to right it
1:41
sounds different from right to left it
1:44
sound
1:45
different from right to left it will m a
1:49
t u a so this is not a palendrome stream
1:52
so this is the program that uh is there
1:58
and now I'll be showing you this program
2:01
step by step inside this Python tutor
2:05
software where it will show you step by
2:08
step how this program execute let me
2:10
paste it here and then click visualize
2:13
executions so first of all it asks you
2:16
to enter a string and we enter a string
2:19
right here let's
2:21
suppose
2:23
T80 once again I write click on submit
2:27
and now you can see the string has been
2:30
stored in a global variable text so this
2:34
variable is
2:35
stored and after that we define a
2:38
variable here is palendrome true so we
2:42
are assuming that the string is a
2:43
palendrome so it's a boolean parameter
2:46
we have declared this variable true now
2:48
for checking this string whether it's a
2:50
palendrome or not we are making use of a
2:53
for loop we click on next so now it
2:57
declares this variable is parentome true
3:00
and now we come to the for loop so for i
3:03
in range and we calculate the length of
3:06
the text which in this case is three
3:08
characters t a t so three characters
3:11
length so the for loop will run
3:15
from we calculate the length of the text
3:17
which is 3 divided by two which will be
3:22
run
3:24
for one time only so it will only run
3:27
for one time because the total length of
3:29
characters is three so 3 divided by two
3:31
comes out to be
3:34
1 so it will only run for one time if I
3:37
go to the for loop so I is equal to zero
3:41
so here we will simply check the very
3:44
first string right here which is t at
3:47
the zero location the string is t so
3:51
right here we check and if it is not
3:55
equal
3:56
to i + 1 so this is negative indexing
4:00
guys in python negative indexing exist
4:03
here you can see we are putting negative
4:07
and i is 0 + 1 so negative-1 so if you
4:12
want to get the rightmost character in
4:14
the string then you write negative -1 so
4:17
this will actually
4:20
uh move to the negative move to the
4:23
rightmost character which is in this
4:25
case is t so in this case t is equal to
4:28
t so this will return true and this will
4:32
not be executed it will come to this
4:34
location
4:37
and if I click next here it will go to
4:40
the next index which is
4:45
a so now the value of so it will
4:49
basically become one so it will not it
4:52
will come out of this loop and this
4:54
condition will is palendrome is true so
4:56
it's already true so now it will print
4:59
out that uh your the string is a
5:03
palindrome so in this easy way you can
5:06
check out basically what we done in this
5:09
we are using negative index right here
5:11
so this
5:13
actually can so if you have a string t a
5:16
t so this negative one that you are
5:19
seeing right here it is accessing to the
5:21
rightmost character so -1 this is this
5:25
is -2 this is
5:27
-3 so negative indexing does exist in
5:30
python so that's why it becomes easy to
5:33
get the rightmost character so we are
5:35
comparing the first character with the
5:38
last character if it's equal to then
5:41
it's a palindrome string because the
5:43
length is only three characters so in
5:45
this case it becomes really easy so
5:48
essentially this is the logic which is
5:50
we are applying in the for loop we are
5:52
running this and we are comparing the
5:54
first character with the rightmost
5:55
character for this we are using negative
5:58
indexing that's all so this is
6:01
the logic for palentum string now we
6:04
come to the number number is also very
6:07
easy so if I run this program
6:11
now so I say Python app 6 so number
6:15
again it's the same if I say 1 to 1
6:17
again it's a palendrome number because
6:19
if you write from left to right it's the
6:22
same if you write from right to left
6:25
it's also same so it's a palendrome
6:27
number so this number is a palendrome if
6:30
I write any other number which sounds
6:36
different let's suppose
6:39
345 so this number is not a palent
6:42
because from right to left it is 5
6:45
43 so now we come to the program so
6:48
essentially we just need to check if the
6:51
reverse of the number is also equal to
6:53
the original number if it's the case
6:55
then we are putting this so now we
6:58
actually first of all take the number so
7:00
I say
7:01
1:1 so this number is stored inside this
7:04
global number variable which is 1:1 and
7:07
then we need to check the reverse of
7:09
this number so for calculating the
7:12
reverse we declare one other variable
7:15
which is reverse equal to zero and for
7:19
this we also have the original number
7:22
which one to one so we are taking two
7:24
variables reverse to zero and original 1
7:26
to
7:27
1 so here we are applying the while loop
7:30
so while the number is greater than zero
7:32
in this case the number is 1 to 1 so
7:35
it's greater than zero now to get the
7:37
rightmost
7:38
character we need to divide the number
7:40
by 10 and this is your percentage to get
7:44
the remainder so this will get the last
7:46
digit because you're dividing the number
7:49
by 10 so the remainder will come out to
7:53
be one so it is stored inside this
7:55
digit and then we
7:58
actually reverse is equal to zero so 0 *
8:02
10 comes out to be 0 0 + 1 so with this
8:07
reverse will be 1 and again we divide
8:10
this number 1 to 1 by 10 so now the
8:13
rightmost characters will be removed now
8:16
it becomes 12 again we check 12 is
8:19
greater than zero again we extract this
8:22
rightmost character again we divide this
8:25
by 10 so it stores the remainder then we
8:29
multiply 1 10 and then you can see so it
8:34
becomes 12 now and then again we divide
8:36
the number by 10 so now it becomes
8:39
1 next step again 1 is greater than zero
8:42
again we divide this this time it it
8:46
will the remainder will come come out to
8:49
be 1
8:51
1 multiplied by 10 120 + 1 which will be
8:56
121 so so the reverse is calculated as
9:00
121 so reverse is equal to the original
9:02
so it it will come out of this while
9:05
loop because number is zero now so now
9:08
the original is equal to the reverse so
9:10
that's why this is actually a palendrome
9:13
number so we print out that this number
9:15
is a palent round so very simple here we
9:18
simply are applying the condition for
9:20
calculating the reverse of the number
9:21
for this we are applying these three
9:23
operations in the while loop first of
9:25
all we divide the number we get the
9:27
remainder then we multiply it by 10 and
9:30
add the remainder and then we divide the
9:33
number so we are doing this inside this
9:35
while loop so once we calculate the
9:38
reverse we are comparing in this if
9:40
condition and if the original is equal
9:41
to reverse then this number is a
9:43
palentro so these two programs are asked
9:47
very commonly inside interviews in
9:49
competitive exams for placements in
9:51
programming interviews
9:53
so in this way you can explain it and uh
9:57
thank you very much for watching this
9:58
video and also check out my website
10:00
freemediatools.com
10:02
uh which contains thousands of tools
#Programming