python removing whitespace from string in a list

前端 未结 6 922
名媛妹妹
名媛妹妹 2021-01-18 10:24

I have a list of lists. I want to remove the leading and trailing spaces from them. The strip() method returns a copy of the string without leading and trailing

6条回答
  •  情歌与酒
    2021-01-18 11:29

    This generates a new list:

    >>> x = ['a', 'b ', ' c  ']
    >>> map(str.strip, x)
    ['a', 'b', 'c']
    >>> 
    

    Edit: No need to import string when you use the built-in type (str) instead.

提交回复
热议问题