Trong hướng dẫn này, chúng tôi sẽ xem xét cách chỉ giữ các chữ cái (trích xuất bảng chữ cái) từ một chuỗi trong Python với sự trợ giúp của các ví dụ. Show Nội dung chính ShowShow
Làm thế nào để trích xuất bảng chữ cái từ một chuỗi trong Python?Trích xuất bảng chữ cái từ một chuỗi bằng regex Sử dụng chức năng chuỗi isalpha() Trích xuất bảng chữ cái từ một chuỗi bằng regexSử dụng chức năng chuỗi isalpha() Làm thế nào để bạn chỉ lọc bảng chữ cái của một chuỗi trong Python?Làm thế nào để bạn trích xuất một chữ cái từ một chuỗi trong Python? import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub(r'[^a-zA-Z]', '', s) print(res) Output: BuckyBarnes Làm thế nào để tôi chỉ cho phép các chữ cái trong Python? Sử dụng chức năng chuỗi isalpha()Làm thế nào để bạn chỉ lọc bảng chữ cái của một chuỗi trong Python?
Làm thế nào để bạn trích xuất một chữ cái từ một chuỗi trong Python? # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res) Output: BuckyBarnes Làm thế nào để tôi chỉ cho phép các chữ cái trong Python? Làm thế nào để bạn lọc một ký tự cụ thể trong Python? # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join([ch for ch in s if ch.isalpha()]) print(res) Output: BuckyBarnes Làm thế nào để bạn chỉ lọc bảng chữ cái của một chuỗi trong Python? Làm thế nào để bạn trích xuất một chữ cái từ một chuỗi trong Python?
Sử dụng str. phương thức isalpha () để kiểm tra xem người dùng chỉ nhập chữ cái chưa .. Nếu điều kiện được đáp ứng, thoát ra khỏi vòng lặp .. Làm thế nào để bạn lọc một ký tự cụ thể trong Python? Sử dụng str. phương thức isalpha () để kiểm tra xem người dùng chỉ nhập chữ cái chưa .. Nếu điều kiện được đáp ứng, thoát ra khỏi vòng lặp .. Làm thế nào để bạn lọc một ký tự cụ thể trong Python? Sử dụng 'str. Thay thế (), chúng ta có thể thay thế một ký tự cụ thể. Nếu chúng ta muốn xóa ký tự cụ thể đó, hãy thay thế ký tự đó bằng một chuỗi trống. STR. Phương thức thay thế () sẽ thay thế tất cả các lần xuất hiện của ký tự cụ thể được đề cập.Method #1 : Using isalpha() + list comprehension The combination of above functions can be used to perform this task. In this, we extract the string which are alphabets only using isalpha() and compile whole logic using list comprehension. Python3Xem thảo luậnĐầu ra: & nbsp; BuckyBarnes3 Sử dụng 'str. Thay thế (), chúng ta có thể thay thế một ký tự cụ thể. Nếu chúng ta muốn xóa ký tự cụ thể đó, hãy thay thế ký tự đó bằng một chuỗi trống. STR. Phương thức thay thế () sẽ thay thế tất cả các lần xuất hiện của ký tự cụ thể được đề cập.Method #1 : Using isalpha() + list comprehension The combination of above functions can be used to perform this task. In this, we extract the string which are alphabets only using isalpha() and compile whole logic using list comprehension. Method #2 : Using filter() + lambda The combination of above methods can be used to perform this task. In this, we perform filtering using filter() and logic for extension to all strings is done using lambda. Python3BuckyBarnes4 BuckyBarnes5 BuckyBarnes6 BuckyBarnes7 BuckyBarnes8 BuckyBarnes9 BuckyBarnes8 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)1__ # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)7 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)8 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)9 BuckyBarnes0 BuckyBarnes1 BuckyBarnes2 BuckyBarnes3 BuckyBarnes5 BuckyBarnes39 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)8 BuckyBarnes31 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)8 BuckyBarnes33 BuckyBarnes34 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)7 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)8 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join([ch for ch in s if ch.isalpha()]) print(res)4 BuckyBarnes0 BuckyBarnes1 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join([ch for ch in s if ch.isalpha()]) print(res)7 Đầu ra: & nbsp; BuckyBarnes3 & nbsp; Phương pháp số 2: Sử dụng Filter () + lambda Sự kết hợp của các phương thức trên có thể được sử dụng để thực hiện tác vụ này. Trong đó, chúng tôi thực hiện lọc bằng cách sử dụng Filter () và logic để mở rộng cho tất cả các chuỗi được thực hiện bằng Lambda. & NBSP; BuckyBarnes54 BuckyBarnes5 BuckyBarnes6 BuckyBarnes7 BuckyBarnes8 BuckyBarnes9 BuckyBarnes8 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)1__O(n)4 BuckyBarnes5 BuckyBarnes6 BuckyBarnes7 BuckyBarnes8 BuckyBarnes9 BuckyBarnes8 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)1__O(n) BuckyBarnes37 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)8 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)9 BuckyBarnes0 BuckyBarnes1 BuckyBarnes2O(n) 7 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)8 # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)9 BuckyBarnes0 BuckyBarnes1 BuckyBarnes2O(n) Sử dụng hàm isalpha () Chức năng Tạo một chuỗi trống để lưu trữ chuỗi kết quả của chúng tôi chỉ bằng các chữ cái. Lặp qua mỗi ký tự trong chuỗi đã cho của chúng tôi. Đối với mỗi ký tự, hãy kiểm tra xem nó có bảng chữ cái sử dụng hàm isalpha () của chuỗi không. Nếu là, sau đó thêm ký tự vào chuỗi kết quả của chúng tôi. Create an empty string to store our result string with only letters. Iterate through each character in our given string. For each character, check if its an alphabet using the string isalpha() function. If it is, then add the character to our result string. Create an empty string to store our result string with only letters. Iterate through each character in our given string. For each character, check if its an alphabet using the string isalpha() function. If it is, then add the character to our result string. Sử dụng 'str.Thay thế (), chúng ta có thể thay thế một ký tự cụ thể.Nếu chúng ta muốn xóa ký tự cụ thể đó, hãy thay thế ký tự đó bằng một chuỗi trống.STR.Phương thức thay thế () sẽ thay thế tất cả các lần xuất hiện của ký tự cụ thể được đề cập.replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned. |