In this article, we will discuss different ways to check the content of the first or last character of a string in python.

Indexing in String

Every character in a string has an index number associated with it. This index can be a positive or negative number. For example, we have a string,

In this, each character has a positive & negative index number associated with it,

The positive index starts from 0 to the size-1. Whereas the negative index for the last element is -1 and it keeps on decremented when we move from right to left. Let’s understand by an example,

For the string “hello text” indexes of characters are as follows,

  • ‘h’ has positive index 0 and negative index -10
  • ‘e’ has positive index 1 and negative index -9
  • ‘l’ has positive index 2 and negative index -8
  • ‘l’ has positive index 3 and negative index -7
  • ‘o’ has positive index 4 and negative index -6
  • ‘ ’ has positive index 5 and negative index -5
  • ‘t’ has positive index 6 and negative index -4
  • ‘e’ has positive index 7 and negative index -3
  • ‘x’ has positive index 8 and negative index -2
  • ‘t’ has positive index 9 and negative index -1

Let’s use this indexing to check conditions on first & last characters of a string.

Check the Last Character of a String in Python

Use negative indexing to check the last character of a string in python

To check the condition on the last character of the string, select the last element using negative index, i.e. -1,

Output:

Calculate size to check the last character of a string in python

To check the condition on the last character of a string,  get the size of the string first. Then check the content of character at index size -1,

Output:

Use endswith() to check the last character of a string in python

String class in python provides a function endswith() which accepts a character or tuple of characters and check if the string ends with the given character or not. Let’s use this to check if the last character of our string is ‘t’,

Output:

Check if the last character of a string matches with any of the given characters in python

endswith() provides an additional facility as compared to the previous solutions. In endswith(), we can pass a tuple of characters too, it will check if the last character of string matches with any of the character in the given tuple or not i.e.

Output:

Check the first character of a string in python

Check the first character of a string using [] operator in python

A string is a sequence of characters and in it, indexing starts from 0. So, to check the first character of a string we need to select the character at 0th index i.e.

Output:

Use startswith() to check the first character of a string in python

String class in python provides a function startswith() which accepts a character or tuple of characters and check if a string starts with the given character or not. Let’s use this to check if the first  character of our string is ‘H’,

Output:

Check the first character of a string matches with any of the given characters

startswith () provides an additional facility. We can pass a tuple of character too, and it will check if the first character of string matches with any of the character in the given tuple or not i.e.

Output:

The complete example is as follows,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.