How to delete the very last character from every string in a list of strings

后端 未结 4 592
长发绾君心
长发绾君心 2021-01-11 12:55

I have the strings \'80010\', \'80030\', \'80050\' in a list, as in

test = [\'80010\',\'80030\',\'80050\']

How can I delete the very last

4条回答
  •  清酒与你
    2021-01-11 13:07

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

提交回复
热议问题