Python Remove Last Character From String

Published December 24, 2020

<>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
# Going to use list comprehension + list slicing in this method

# initialize the list 
original_list = ['Rohan', 'Rohit', 'Albert', 'Robert']

# To print an initialized list
print("The original list : " + str(original_list))

# To remove last character from list of strings and update the initialized string
updated_list = [sub[ : -1] for sub in original_list]

# print the result
print("The list after removing last characters : " + str(updated_list))

 

<>Output

<>The original list : ['Rohan', 'Rohit', 'Albert', 'Robert']  
The list after removing last characters : ['Roha', 'Rohi', 'Alber', 'Rober']  

 

<>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

# initialize the list
original_list = ['Alex', 'Briden', 'Boomer', 'Alia']

# To print the initialized list
print("The original list : " + str(original_list))

# By using map() + lambda method
# Now remove the last character from list of strings
updated_list = list(map(lambda i: i[ : -1], original_list))

# Print the updated result
print("The list after removing last characters : " + str(updated_list))

 

<>Output

<>The original list : ['Alex', 'Briden', 'Boomer', 'Alia']                                                           
The list after removing last characters : ['Ale', 'Bride', 'Boome', 'Ali']

 

<>Read More about Python strings Here 

Remove last character from string in python

<>With the bove methods not only last string we can remove any character from string in python

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1810 Views