2

I found an awesome palindrome code and I can understand abstractly, but I'm not sure how it really works.

def palindrome(word):
    return word == word[::-1]

I thought the data type of word is 'string' not 'list'. But in that code, it treats word as a list type. Does it automatically change word to list(word)?

Dalsoom
  • 23
  • 3

4 Answers4

2

Python slice notation is pretty straightforward. Taken from another answer:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array
a[start:stop:step] # start through not past stop, by step

So with an example:

word = 'abcd'

assert word[0] == 'a'
assert word[0:3] == 'abc'
assert word[0:4] == 'abcd'
assert word[0:4:2] == 'ac'

In your case if step is -1 then it goes backwards:

assert word[::-1] = 'dcba'

So if a word backward is equal to word itself then it is palindrome:

if word == word[::-1]:
    return True
ramazan polat
  • 7,111
  • 1
  • 48
  • 76
0

Strings behave like sequences which are sliceable.

ndclt
  • 2,590
  • 2
  • 12
  • 26
  • Does that mean you can use slicing on any iterable? I don't think that's the case. – Mark Ransom Sep 15 '19 at 14:16
  • You are right (see this [question](https://stackoverflow.com/questions/34732311/how-to-slice-a-generator-object-or-iterator-in-python)), it looks like slicing work on sequences (I edit my answer). – ndclt Sep 15 '19 at 14:23
0

One way to look at Python strings is as ordered sequences of characters. They support indexing and slicing with the square bracket notation, like s[...]. This also means you can loop over them, and use the in test for membership.

Have a look at this question: What exactly are iterator, iterable, and iteration?

Matt Hall
  • 7,614
  • 1
  • 23
  • 36
  • Thanks for your answer. Actually, I'm confused of using [] on strings. Than is 'ape' treated equally as ['a','p','e'] in bracket notation? – Dalsoom Sep 15 '19 at 14:27
  • More or less, as far as indexing and slicing are concerned. But strings have different _methods_ (functions attached to them). So you have `s.upper()` for example. But there's no `append` method (among other things), because you can't change a string in-place. Try doing `s = 'hello'; s[1] = 'a'` and you'll see you can't change a string like that. (But you can change lists.) – Matt Hall Sep 15 '19 at 14:29
0

A subsequence of a sequence is called a slice and the operation that extracts a subsequence is called slicing. Like with indexing, we use square brackets ([ ]) as the slice operator, but instead of one integer value inside we have two, seperated by a colon (:):

>>> singers = "Peter, Paul, and Mary"
>>> singers[0:5]
'Peter'
>>> singers[7:11]
'Paul'
>>> singers[17:21]
'Mary'
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[2:4]
('Kathryn', 'Presila')

The operator [n:m] returns the part of the sequence from the n’th element to the m’th element, including the first but excluding the last. This behavior is counter-intuitive; it makes more sense if you imagine the indices pointing between the characters, as in the following diagram:

banana

'banana' string

If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string. Thus:

>>> fruit = "banana"
>>> fruit[:3]
'ban'
>>> fruit[3:]
'ana'

What do you think s[:] means? What about classmates[4:]?

Negative indexes are also allowed, so

>>> fruit[-2:]
'na'
>>> classmates[:-2]
('Alejandro', 'Ed', 'Kathryn', 'Presila')

source

Shahab Rahnama
  • 982
  • 1
  • 7
  • 14