Remove final character from string (Python)

This question already has an answer here:

  • Understanding Python's slice notation 29 answers

  • Simple:

    st =  "abcdefghij"
    st = st[:-1]
    

    there is also another way that show how it is done with steps:

    list1 = "abcdefghij"
    list2 = list(list1)
    print (list2)
    list3 = list2[:-1]
    print (list3)
    

    This is also a way with the user inputting a word:

    list1 = input ("Enter :")
    list2 = list(list1)
    print (list2)
    list3 = list2[:-1]
    print (list3)
    

    To make it take away the last word in a list:

    list1 = input ("Enter :")
    list2 = list1.split()
    print (list2)
    list3 = list2[:-1]
    print (list3)
    
    链接地址: http://www.djcxy.com/p/26726.html

    上一篇: 在Python列表索引中的冒号(:)

    下一篇: 从字符串中移除最后一个字符(Python)