Python: How to remove [' and ']?

前端 未结 4 695
说谎
说谎 2021-01-29 11:36

I want to remove [\' from start and \'] characters from the end of a string.

This is my text:

\"[\'45453656565\']\"
         


        
4条回答
  •  旧时难觅i
    2021-01-29 11:51

    You need to strip your text by passing the unwanted characters to str.strip() method:

    >>> s = "['45453656565']"
    >>> 
    >>> s.strip("[']")
    '45453656565'
    

    Or if you want to convert it to integer you can simply pass the striped result to int function:

    >>> try:
    ...     val = int(s.strip("[']"))
    ... except ValueError:
    ...     print("Invalid string")
    ... 
    >>> val
    45453656565
    

提交回复
热议问题