zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【Python】删除字符串首尾的空字符 空格 回车 换行等

Python 字符串 删除 空格 换行 回车
2023-09-11 14:16:50 时间

 

    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