Python入門題044:刪除可見和不可見的空白字元

題目:

字串包含可見的空格、製表符,以及不可見的回車符、換行符,和 unicode 的 \x00 空白符。要求全部刪除。

#python #字串 #不可見字元

影片教程:

Python入門題044:刪除可見和不可見的空白字元

程式碼:

import reline = ‘ ab\td\x00efg\n\n\n hi\nworld\t’print(line)print(repr(line))# ‘ ab\td\x00efg\n\n\n hi\nworld\t’line = ‘ ab\td\x00efg\n\n\n hi\nworld\t’line = line。strip()print(repr(line))# ‘ab\td\x00efg\n\n\n hi\nworld’line = ‘ ab\td\x00efg\n\n\n hi\nworld\t’line = re。sub(r‘\s’, ‘’, line)print(repr(line))# ‘abd\x00efghiworld’line = ‘ ab\td\x00efg\n\n\n hi\nworld\t’line = re。sub(r‘(\s|\x00)’, ‘’, line)print(repr(line))# ‘abdefghiworld’