Check the First or Last Character of a String in Python

In Python, strings are sequences of bytes that represent Unicode characters. Due to the lack of a character data form in Python, a single character is simply a one-length string. To access the string’s components, use square brackets.

Examples:

Checking the last character:

Input:

string="BTechGeeks" lastcharacter='s'

Output:

Last character of string endswith s

Input:

string="BTechGeeks" lastcharacter='p'

Output:

Last character of string do no not end with p

Checking the first character:

Input:

string = 'BTechGeeks'  firstchar = 'B'

Output:

First character of string start with B

Input:

string = 'BTechGeeks'  firstchar = 'r'

Output:

First character of string do no not start with r

Checking the Last and First Character of the String

There are several ways to check first and last character of the string some of them are:

Checking last Character:

Method #1: Using negative slicing

Select the last element with a negative index to verify the condition on the string’s last character.

Below is the implementation:

# given string
string = 'BTechGeeks'
lastchar = 'p'
# check the last character ends with s
if(string[-1] == lastchar):
    print('Last character of string endswith', lastchar)
else:
    print('Last character of string do no not end with', lastchar)

Output:

Last character of string endswith s

Method #2:Using length of string

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

Below is the implementation:

string = 'BTechGeeks'
lastchar = 's'
# calculating length of string
length = len(string)
# check the last character ends with s
if(string[length-1] == lastchar):
    print('Last character of string endswith', lastchar)
else:
    print('Last character of string do no not end with', lastchar)

Output:

Last character of string endswith s

Method #3:Using endswith()

If a string ends with the specified suffix, the endswith() method returns True; otherwise, it returns False.

Below is the implementation:

string = 'BTechGeeks'
lastchar = 's'
# calculating length of string
length = len(string)
# check the last character ends with s using endswith()
if(string.endswith(lastchar)):
    print('Last character of string endswith', lastchar)
else:
    print('Last character of string do no not end with', lastchar)

Output:

Last character of string endswith s

Method #4: Checking whether the string’s last character matches any of the given multiple characters

In comparison to the previous solutions, endswith() adds a new function. We may also move a tuple of characters to endswith(), which will check whether the last character of the string matches any of the characters in the specified tuple or not.

Below is the implementation:

# Given string and characters
string = 'BTechGeeks'
lastchars = ('s', 'p', 'e')
# check the first character of the given string
if(string.endswith(lastchars)):
    print('Given string ends with one of the given characters')

Output:

Given string ends with one of the given characters

Checking First Character:

Method #5:Using index 0 to get first character

A string is a set of characters, with indexing starting at 0. To find the first character in a string, select the character at the 0th index.

Below is the implementation:

# Given string and character
string = 'BTechGeeks'
firstchar = 'B'
# check the first character of the given string
if(string[0] == firstchar):
    print('First character of string start with', firstchar)
else:
    print('First character of string do no not start with', firstchar)

Output:

First character of string start with B

Method #6:Using startswith()

Python’s String class has a function called startswith() that takes a character or a tuple of characters and checks whether a string begins with that character or not. Let’s use this to see if our string’s first character is ‘B’

Below is the implementation:

# Given string
string = 'BTechGeeks'
firstchar = 'B'
# check the first character of the given string
if(string.startswith(firstchar)):
    print('First character of string start with', firstchar)
else:
    print('First character of string do no not start with', firstchar)

Output:

First character of string start with B

Method #7:Checking whether the string’s last character matches any of the given multiple characters

startswith () adds a extra function . We may also transfer a character tuple, and it will check if the first character of the string matches any of the characters in the tuple.

Below is the implementation:

# Given string and first char
string = 'BTechGeeks'
firstchars = ('s', 'p', 'B')
# check the first character of the given string
if(string.startswith(firstchars)):
    print('given string starts with one of the given characters')

Output:

given string starts with one of the given characters

Related Programs: