【Python】刪除字符串首尾的空字符 空格 回車 換行等


 

    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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM