How can I remove the last character of a string in python?

This question already has an answer here:

  • Is there a way to substring a string in Python? 10 answers

  • As you say, you don't need to use a regex for this. You can use rstrip .

    my_file_path = my_file_path.rstrip('/')
    

    If there is more than one / at the end, this will remove all of them, eg '/file.jpg//' -> '/file.jpg' . From your question, I assume that would be ok.


    The easiest is

    as @greggo pointed out

    string="mystring";
    string[:-1]
    

    你可以使用String.rstrip

    result = string.rstrip('/')
    
    链接地址: http://www.djcxy.com/p/55112.html

    上一篇: 在Python中打印字符串的特定部分

    下一篇: 我怎样才能删除python中的字符串的最后一个字符?