首页 > 技术文章 > 【Python】删除字符串首尾的空字符 空格 回车 换行等

danhuai 2020-11-02 13:47 原文

 

    def del_endswith_none(str1: str):
        """
        删除字符串首尾的空字符(空格、换行)
        :param str1:
        """
        s = str1
        if str1 is not None:
            while True:
                if s.endswith(' ') or s.endswith('    ') or s.endswith('\r\n') or s.endswith('\r') or s.endswith('\n'):
                    s = s[:-1]
                elif s.startswith(' ') or s.startswith('    ') or s.startswith('\r\n') or s.startswith('\r') or s.startswith('\n'):
                    s = s[1:]
                else:
                    break
        return s

 

推荐阅读