{"id":3641,"date":"2023-10-23T19:28:48","date_gmt":"2023-10-23T13:58:48","guid":{"rendered":"https:\/\/python-programs.com\/?p=3641"},"modified":"2023-11-10T11:55:36","modified_gmt":"2023-11-10T06:25:36","slug":"check-the-first-or-last-character-of-a-string-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/check-the-first-or-last-character-of-a-string-in-python\/","title":{"rendered":"Check the First or Last Character of a String in Python"},"content":{"rendered":"

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.<\/p>\n

Examples:<\/strong><\/p>\n

Checking the last character:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

string=\"BTechGeeks\" lastcharacter='s'<\/pre>\n

Output:<\/strong><\/p>\n

Last character of string endswith s<\/pre>\n

Input:<\/strong><\/p>\n

string=\"BTechGeeks\" lastcharacter='p'<\/pre>\n

Output:<\/strong><\/p>\n

Last character of string do no not end with p<\/pre>\n

Checking the first character:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

string = 'BTechGeeks'\u00a0 firstchar = 'B'<\/pre>\n

Output:<\/strong><\/p>\n

First character of string start with B<\/pre>\n

Input:<\/strong><\/p>\n

string = 'BTechGeeks'\u00a0 firstchar = 'r'<\/pre>\n

Output:<\/strong><\/p>\n

First character of string do no not start with r<\/pre>\n

Checking the Last and First Character of the String<\/h2>\n

There are several ways to check first and last character of the string some of them are:<\/p>\n