<>As a python programmer sometimes we are stuck in such a situation that we have to remove the last character from the string that is added by mistake or as an additional. During this problem what we basically do is remove the whole string and enter the write one. It usually occurs in web development. Is there any easy way to remove last character from string in python? The answer is yes.
<>Today we are going to learn 2 methods that will definitely help you in removing the last character from string without deleting or eliminating the whole string. Here are the 2 methods we are going to learn today in this python tutorial.
<>Using List Comprehension and List Slicing
<>Using Map Function "map()" + Lambda
<>Let's know about how to use these methods in python to remove the last character from the string briefly
<>Python Remove Last Character From String
<>Method 1: Using List Comprehension and List Slicing
<>This method includes list slicing to remove the characters and the list comprehension that helps in extending the whole list in python. Here is a program for this method
<># Python code to remove last character from string |
<>Output
<>The original list : ['Rohan', 'Rohit', 'Albert', 'Robert'] |
<>Method 2: Using Map Function "map()" + Lambda
<>The map function usually helps the task of getting the functionality executed for all the members on the list. The lambda function helps the task to remove the last element using list comprehension. Here is a program to implement this method
<># Python code to remove last character from string using map() + lambda method |
<>Output
<>The original list : ['Alex', 'Briden', 'Boomer', 'Alia'] |
<>Read More about Python strings Here
<>With the bove methods not only last string we can remove any character from string in python
Article Contributed By :
|
|
|
|
2095 Views |