{"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 Output:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Checking the first character:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to check first and last character of the string some of them are:<\/p>\n Select the last element with a negative index to verify the condition on the string’s last character.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If a string ends with the specified suffix, the endswith() method returns True; otherwise, it returns False.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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’<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n startswith () adds a extra\u00a0function . 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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” …<\/p>\nstring=\"BTechGeeks\" lastcharacter='s'<\/pre>\n
Last character of string endswith s<\/pre>\n
string=\"BTechGeeks\" lastcharacter='p'<\/pre>\n
Last character of string do no not end with p<\/pre>\n
string = 'BTechGeeks'\u00a0 firstchar = 'B'<\/pre>\n
First character of string start with B<\/pre>\n
string = 'BTechGeeks'\u00a0 firstchar = 'r'<\/pre>\n
First character of string do no not start with r<\/pre>\n
Checking the Last and First Character of the String<\/h2>\n
\n
Checking last Character:<\/h3>\n
Method #1: Using negative slicing<\/h3>\n
# given string\r\nstring = 'BTechGeeks'\r\nlastchar = 'p'\r\n# check the last character ends with s\r\nif(string[-1] == lastchar):\r\n print('Last character of string endswith', lastchar)\r\nelse:\r\n print('Last character of string do no not end with', lastchar)\r\n<\/pre>\n
Last character of string endswith s<\/pre>\n
Method #2:Using length of string<\/h3>\n
string = 'BTechGeeks'\r\nlastchar = 's'\r\n# calculating length of string\r\nlength = len(string)\r\n# check the last character ends with s\r\nif(string[length-1] == lastchar):\r\n print('Last character of string endswith', lastchar)\r\nelse:\r\n print('Last character of string do no not end with', lastchar)\r\n<\/pre>\n
Last character of string endswith s<\/pre>\n
Method #3:Using endswith()<\/h3>\n
string = 'BTechGeeks'\r\nlastchar = 's'\r\n# calculating length of string\r\nlength = len(string)\r\n# check the last character ends with s using endswith()\r\nif(string.endswith(lastchar)):\r\n print('Last character of string endswith', lastchar)\r\nelse:\r\n print('Last character of string do no not end with', lastchar)\r\n<\/pre>\n
Last character of string endswith s<\/pre>\n
Method #4: Checking whether the string’s last character matches any of the given multiple characters<\/h3>\n
# Given string and characters\r\nstring = 'BTechGeeks'\r\nlastchars = ('s', 'p', 'e')\r\n# check the first character of the given string\r\nif(string.endswith(lastchars)):\r\n print('Given string ends with one of the given characters')\r\n<\/pre>\n
Given string ends with one of the given characters<\/pre>\n
Checking First Character:<\/h3>\n
Method #5:Using index 0 to get first character<\/h3>\n
# Given string and character\r\nstring = 'BTechGeeks'\r\nfirstchar = 'B'\r\n# check the first character of the given string\r\nif(string[0] == firstchar):\r\n print('First character of string start with', firstchar)\r\nelse:\r\n print('First character of string do no not start with', firstchar)\r\n<\/pre>\n
First character of string start with B<\/pre>\n
Method #6:Using startswith()<\/h3>\n
# Given string\r\nstring = 'BTechGeeks'\r\nfirstchar = 'B'\r\n# check the first character of the given string\r\nif(string.startswith(firstchar)):\r\n print('First character of string start with', firstchar)\r\nelse:\r\n print('First character of string do no not start with', firstchar)\r\n<\/pre>\n
First character of string start with B<\/pre>\n
Method #7:Checking whether the string’s last character matches any of the given multiple characters<\/h3>\n
# Given string and first char\r\nstring = 'BTechGeeks'\r\nfirstchars = ('s', 'p', 'B')\r\n# check the first character of the given string\r\nif(string.startswith(firstchars)):\r\n print('given string starts with one of the given characters')\r\n<\/pre>\n
given string starts with one of the given characters<\/pre>\n
\n