{"id":3179,"date":"2021-04-21T07:39:15","date_gmt":"2021-04-21T02:09:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=3179"},"modified":"2021-11-22T18:45:02","modified_gmt":"2021-11-22T13:15:02","slug":"python-how-to-get-last-n-characters-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-get-last-n-characters-in-a-string\/","title":{"rendered":"Python: How to get Last N characters in a string?"},"content":{"rendered":"

This article is about finding the last N characters of String where the N represents any number or int value.<\/p>\n

How to get the last N characters in a String using Python<\/h2>\n

In Python, String is a sequence of characters where character can be a string, integer or any symbol. With each character the index numbers are associated with each character.<\/p>\n

For example : We have a String variable named String_value<\/code> which contains a String Technology i.e<\/p>\n

String_value = \u201cDigital\u201d<\/pre>\n

The first character starts with index 0 and it goes on. Like<\/p>\n

Index of character \u2018D\u2019 = 0<\/p>\n

Index of character \u2018i\u2019 = 1<\/p>\n

Index of character \u2018g\u2019 = 2<\/p>\n

Index of character \u2018i\u2019 = 3<\/p>\n

Index of character \u2018t\u2019 = 4<\/p>\n

Index of character \u2018a\u2019 = 5<\/p>\n

Index of character \u2018l\u2019\u00a0 = 6<\/p>\n

In Python, just by passing the index number in [ ] (Subscript operator or Index Operator) any character of the string can be accessed.<\/p>\n

Like String_value[i] will return i-th character of the string, where ‘i’ represents an index position.<\/p>\n

For example, String_value[4] will return character \u2018t\u2019.<\/p>\n

Get last character of the String using negative indexing :<\/h3>\n

Negative indexing refers to associate negative index value to each character of the string. Like last character of string will be associated with index number -1 then negative index will go on incrementing and characters will go on decrementing so that each character will be associated with a negative index.<\/p>\n

Let’s see how actually its implemented<\/p>\n

String_value = \u201cDigital\u201d<\/pre>\n

Where the sequence number\/index of the last character starts with -1 and it goes on. Like<\/p>\n

Index of character \u2018D\u2019 = -7<\/p>\n

Index of character \u2018i\u2019 = -6<\/p>\n

Index of character \u2018g\u2019 = -5<\/p>\n

Index of character \u2018i\u2019 = -4<\/p>\n

Index of character \u2018t\u2019 = -3<\/p>\n

Index of character \u2018a\u2019 = -2<\/p>\n

Index of character \u2018l\u2019\u00a0 = -1<\/p>\n

So let’s see a program to get the last character of the string using negative indexing.<\/p>\n

#Program:\r\n\r\nString_value = \"Digital\"\r\nLast_char = String_value[-1]\r\nprint('Last character of the string is', Last_char)<\/pre>\n
Output : <\/b>\r\nLast character of the string is l<\/span><\/pre>\n

Here, just by passing -1 in the operator [ ], we fetched the last character of the string.<\/span><\/p>\n

Get the last character of string using len() function :<\/h3>\n

Without using negative indexing we can also get the last character of the string just by finding the length of the string and then printing the character at length-1 position.<\/p>\n

So let’s see a program to get the last character of the string using len( ) function<\/code>.<\/p>\n

#Program: \r\n\r\nString_value = \"Digital\" \r\n#Finding length of String_value\r\nlength = len(String_value )\r\n# Finding the last character of string\r\n#Length of the string -1 represents last character\r\nLast_char = String_value[length -1]\r\nprint('Last character of the string is', Last_char)\r\n<\/pre>\n
Output :\r\nLast character of the string is l<\/pre>\n

Get last N characters in a string :<\/h3>\n

In above examples we only have passed index position in the subscript operator, i.e. [] but it can also accept a range too i.e.<\/p>\n

\n
\n
\n
Syntax : string_value<\/span>[<\/span> start_index_position : end_index_position : step_size<\/span>]<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n

Where,<\/p>\n