How to use list comprehension and join method In Python
Algorithm:
- Initialize the given string “Str”.
- Create a list “x” by iterating through each character in the string “Str” using a list comprehension.
- Remove the last character from the list “x” using the pop() method.
- Join the characters in the list “x” back to form a string and store it in “x”.
- Print the resulting string “x”.
Python3
#Python program to Remove Last character from the string Str = "w3wiki" x = "".join([ Str [i] for i in range ( len ( Str ) - 1 )]) print (x) |
GeeksForGeek
Time Complexity: O(n), where n is the length of the input string “Str”.
Auxiliary Space: O(n), where n is the length of the Out[ut string “x”.
Python program to Remove Last character from the string
Given a string, the task is to write a Python program to remove the last character from the given string.
Example:
Input: “w3wiki”
Output: “GeeksForGeek”Input: “1234”
Output: “123”
Explanation: Here we are removing the last character of the original string.
Note: Strings are immutable in Python, so any modification in the string will result in the creation of a new string.