The best and easy way to find the last character in a string is negative indexing. Every character in a string has an index number associated that can be a positive or negative number.
Python find the last character in a string example
Simple example code selects the last element using the negative index, i.e. -1.
sample_str = 'Hello World' print(sample_str[-1])
Return the last character of string in python
If the given string is multiline then it last character of every line is a newline character. We have to clear its leading and trailing spaces. Then return the last character in your case.
Use the strip method in this case.
sample_str = 'Hello World ' \ '' print(sample_str.strip()[-1])
Do comment if you have any doubts and suggestions on this Python find char program.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.