test = ["80010","80030","80050"]
newtest = [x[:-1] for x in test]
New test will contain the result ["8001","8003","8005"].
[x[:-1] for x in test] creates a new list (using list comprehension) by looping over each item in test and putting a modified version into newtest. The x[:-1] means to take everything in the string value x up to but not including the last element.
test = ["80010","80030","80050"]
newtest = [x[:-1] for x in test]
New test will contain the result ["8001","8003","8005"].
[x[:-1] for x in test] creates a new list (using list comprehension) by looping over each item in test and putting a modified version into newtest. The x[:-1] means to take everything in the string value x up to but not including the last element.
You are not so far off. Using the slice notation [:-1] is the right approach. Just combine it with a list comprehension:
>>> test = ['80010','80030','80050']
>>> [x[:-1] for x in test]
['8001', '8003', '8005']
somestring[:-1] gives you everything from the character at position 0 (inclusive) to the last character (exclusive).
python - Remove final character from string - Stack Overflow
5 Ways You Can Remove Last Character From String in Python
arrays - Removing last character from each element in Python list - Stack Overflow
Remove last 3 characters of each element of a list of strings.
Simple:
my_str = "abcdefghij"
my_str = my_str[:-1]
Try the following code snippet to better understand how it works by casting the string as a list:
str1 = "abcdefghij"
list1 = list(str1)
print(list1)
list2 = list1[:-1]
print(list2)
In case, you want to accept the string from the user:
str1 = input("Enter :")
list1 = list(str1)
print(list1)
list2 = list1[:-1]
print(list2)
To make it take away the last word from a sentence (with words separated by whitespace like space):
str1 = input("Enter :")
list1 = str1.split()
print(list1)
list2 = list1[:-1]
print(list2)
What you are trying to do is an extension of string slicing in Python:
Say all strings are of length 10, last char to be removed:
>>> st[:9]
'abcdefghi'
To remove last N characters:
>>> N = 3
>>> st[:-N]
'abcdefg'
You can do this simply with a list comprehension and str.rstrip():
[word.rstrip("$") for word in words]
Or to add them:
[word+"$" for word in words]
E.g:
>>> words = ['Duration$', 'Noun$', 'Adjective$']
>>> words = [word.rstrip("$") for word in words]
>>> words
['Duration', 'Noun', 'Adjective']
>>> [word+"$" for word in words]
['Duration$', 'Noun$', 'Adjective$']
words = [ x[:-1] for x in words ]
will remove last character from each item
I have a list of strings, call it S, where each element has a format similar to '2016-02-27 04:49:29.766684-06โ. I want to remove the last three characters from each element of the list so that it looks similar to '2016-02-27 04:49:29.766684โ. In this case the last three characters were '-06' but sometimes they are '-05' or some other number following the dash. This is easy to do in R but I can't figure it out in Python.
Slicing is the best way to go about that.
test_string = 'This is a test!'
print (test_string [1:-1])
From your response that removing the first and last word would be done by list1.pop() and list1.pop(0) I am assuming that you are given a list of strings and are required to remove the first and last character of each string in the list.
If my assumption is correct you can do a combination of slicing and list comprehension.
list_of_str = ["words", "words", "words", "words"]
list_of_shortened_str = [word[1:-1] for word in list_of_str]
# list_of_shortened_str = ["ord", "ord", "ord", "ord"]