สร้างอาร์เรย์เก็บตัวอักษรที่ไม่ใช่ตัวเลขในสตริง แล้ว join ด้วย "".join()
ตัวอย่าง
my_str = 'qwerty123asdf32'
non_digits = []
for c in my_str:
if not c.isdigit():
non_digits.append(c)
result = ''.join(non_digits)
print(result)
ผลลัพธ์
qwertyasdf
ตัวอย่าง
ทำได้ในบรรทัดเดียวด้วย list comprehension
my_str = 'qwerty123asdf32' result = ''.join([c for c in my_str if not c.isdigit()]) print(result)
ผลลัพธ์
qwertyasdf