1

How can I get the last character of this string?

seed_name = "Cocoa"
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
gabriel-pimentel
  • 81
  • 1
  • 1
  • 9
  • 3
    [Slicing](https://stackoverflow.com/q/509211/5472354): `seed_name[-1]`. – mapf Sep 30 '21 at 22:40
  • 2
    @mapf But it's *not* slicing, but indexing. (yes, `[-1]` appears in the answer over there but technically it shouldn't) – mkrieger1 Sep 30 '21 at 22:41
  • @mkrieger1 fair enough. I always felt like indexing is just a subset of slicing. – mapf Sep 30 '21 at 22:42
  • Relevant meta question: https://meta.stackoverflow.com/questions/374701/what-to-do-when-the-question-is-so-trivial-rtfm-that-it-doesnt-even-have-a-prop – mkrieger1 Sep 30 '21 at 22:49
  • 2
    @BhargavRao https://stackoverflow.com/questions/5163785/how-do-i-get-the-last-character-of-a-string is Java, not Python; https://stackoverflow.com/questions/15478127/remove-final-character-from-string "remove the last character" is something else than "return [or get] the last character". – mkrieger1 Oct 01 '21 at 00:27
  • 1
    I also don't think "[-1]" can be an answer to https://stackoverflow.com/questions/663171/how-do-i-get-a-substring-of-a-string-in-python while it is *the* answer here – mkrieger1 Oct 01 '21 at 00:34

1 Answers1

12

As shown in the official Python tutorial,

>>> word = 'Python'

[...]

Indices may also be negative numbers, to start counting from the right:

>>> word[-1]  # last character
'n'
mkrieger1
  • 19,194
  • 5
  • 54
  • 65