Use the slice notation
[ ] with
1:-1 as an argument to trim the first and last character. Where 1 represents the second character index (included) and -1 represents the last character index (excluded).
Example trim the first and last character from a string in Python
Simple example code.
str1 = "Hello world!" print(str1[1:-1])
Note: There are two types of indexes in python:-
- Left to right or Positive Index
- Right to left or Negative Index
Do comment if you have any doubts and suggestions on this Python string trim topic.
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.