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

วิธีลบตัวอักษรเฉพาะออกจาก String ใน Python

เนื้อหาเกือบเหมือน ID 1004108041

1. ใช้ replace()

>> "Hello people".replace("e", "")
'Hllo popl'

2. ใช้ regex re.sub()

import re
print(re.sub("e|l", " ", "Hello people"))

ผลลัพธ์

H  o p op

หมายเหตุ: ใช้ [] สร้าง character class ใน regex ได้ เช่น [el]