Bạn có thể tạo mảng lưu tất cả ký tự không phải chữ số trong chuỗi. Sau đó nối mảng này bằng "".join().
Ví dụ
my_str = 'qwerty123asdf32' non_digits = [] for c in my_str: if not c.isdigit(): non_digits.append(c) result = ''.join(non_digits) print(result)
Kết quả
Điều này sẽ cho kết quả
qwertyasdf
Ví dụ
Cũng có thể làm trong một dòng với list comprehension.
my_str = 'qwerty123asdf32' result = ''.join([c for c in my_str if not c.isdigit()]) print(result)
Kết quả
Điều này sẽ cho kết quả
qwertyasdf