Example Get last two characters of string in Python
Simple example code. It will slice the starting char excluding the last 2 characters.
str1 = "Hello world" l = len(str1) print(str1[l - 2:])
Get the last character of string using len() function
Calculate string length first and then access character at length -1.
str1 = "Hello world" l = len(str1) print(str1[l - 1])
Do comment if you have any doubts and suggestions on this Python char code.
Note: IDE: PyCharm 2021.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.