Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี strip spaces/tabs/newlines ด้วย Python Regular Expression

ใช้ regex strip spaces/tabs/newlines จากสตริง ใช้ \S แทน non-whitespace characters ทั้งหมด

ตัวอย่าง

import re
print(re.findall(r"[\S]", """I find     Tutorialspoint useful"""))

ผลลัพธ์

['I', 'f', 'i', 'n', 'd', 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't', 'u', 's', 'e', 'f', 'u', 'l']