Hướng dẫn how to get only alphabets from string in python - cách chỉ lấy bảng chữ cái từ chuỗi trong python

Trong Python, tôi chỉ muốn trích xuất các ký tự từ một chuỗi.

Xem xét tôi có chuỗi sau,

input = "{['players',]: 24, ['year',]: 28, ['money',]: 19, ['ipod',]: 36, ['case',]: 23, ['mini',]: 46}"

Tôi muốn kết quả như,

output =  "players year money ipod case mini"

Tôi đã cố gắng phân chia chỉ xem xét các bảng chữ cái,

word1 = st.split["[a-zA-Z]+"]

Nhưng sự chia rẽ không xảy ra.

& nbsp; Phương pháp #3: Sử dụng

output =  "players year money ipod case mini"
2

word1 = st.split["[a-zA-Z]+"]
7
word1 = st.split["[a-zA-Z]+"]
8
word1 = st.split["[a-zA-Z]+"]
9
output =  "players year money ipod case mini"
01

Bạn có thể sử dụng một biểu thức thông thường để chỉ trích xuất các chữ cái [bảng chữ cái] từ một chuỗi trong Python. Bạn cũng có thể lặp lại các ký tự trong một chuỗi và sử dụng hàm chuỗi

output =  "players year money ipod case mini"
2 để chỉ giữ các chữ cái trong một chuỗi.

Hãy cùng xem xét cả hai phương pháp với sự trợ giúp của các ví dụ -

Trích xuất bảng chữ cái từ một chuỗi bằng regex

Bạn có thể sử dụng biểu thức thông thường

output =  "players year money ipod case mini"
3 để khớp với các ký tự không phải alphabet trong chuỗi và thay thế chúng bằng một chuỗi trống bằng hàm
output =  "players year money ipod case mini"
4. Chuỗi kết quả sẽ chỉ chứa các chữ cái.

Hãy cùng nhìn vào một ví dụ.

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

Bạn có thể thấy rằng chuỗi kết quả chỉ chứa các chữ cái.

Sử dụng chức năng chuỗi
output =  "players year money ipod case mini"
2

Ngoài ra, bạn có thể sử dụng hàm chuỗi

output =  "players year money ipod case mini"
2 để xóa các ký tự không alphabet khỏi chuỗi. Sử dụng các bước sau -

  1. 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.
  2. Lặp qua mỗi ký tự trong chuỗi đã cho của chúng tôi.
  3. Đối với mỗi ký tự, hãy kiểm tra xem nó có bảng chữ cái sử dụng chức năng chuỗi
    output =  "players year money ipod case mini"
    
    2 không. Nếu là, sau đó thêm ký tự vào chuỗi kết quả của chúng tôi.

Hãy cùng nhìn vào một ví dụ.

# 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

Bạn có thể thấy rằng chuỗi kết quả chỉ chứa các chữ cái.

Sử dụng chức năng chuỗi

output =  "players year money ipod case mini"
2

# 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

Ngoài ra, bạn có thể sử dụng hàm chuỗi

output =  "players year money ipod case mini"
2 để xóa các ký tự không alphabet khỏi chuỗi. Sử dụng các bước sau -

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 chức năng chuỗi
    output =  "players year money ipod case mini"
    
    2 không. Nếu là, sau đó thêm ký tự vào chuỗi kết quả của chúng tôi.
  • Chuỗi kết quả chỉ chứa các chữ cái từ chuỗi gốc.


Mã trên có thể được giảm xuống ít dòng hơn bằng cách sử dụng danh sách hiểu.
We do not spam and you can opt out any time.

  • Chúng tôi nhận được kết quả tương tự như trên.

    Bạn cũng có thể quan tâm đến -

Làm thế nào để bạn chỉ chấp nhận bảng chữ cái trong Python?

Chỉ cho phép các chữ cái khi lấy đầu vào của người dùng trong Python #.

Sử dụng vòng lặp thời gian để lặp lại cho đến khi người dùng nhập các chữ cái ..

  • 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 chỉ chấp nhận bảng chữ cái trong Python?

    Chỉ cho phép các chữ cái khi lấy đầu vào của người dùng trong Python #.

    Sử dụng vòng lặp thời gian để lặp lại cho đến khi người dùng nhập các chữ cái ..

    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 ..

    output =  "players year money ipod case mini"
    
    9
    word1 = st.split["[a-zA-Z]+"]
    
    0

    word1 = st.split["[a-zA-Z]+"]
    
    1
    word1 = st.split["[a-zA-Z]+"]
    
    2
    word1 = st.split["[a-zA-Z]+"]
    
    3

    word1 = st.split["[a-zA-Z]+"]
    
    4
    word1 = st.split["[a-zA-Z]+"]
    
    2
    word1 = st.split["[a-zA-Z]+"]
    
    6

    Làm thế nào để tôi có được bảng chữ cái trong Python?

    Cách dễ nhất để tải một danh sách tất cả các chữ cái của bảng chữ cái là sử dụng chuỗi.ascii_letters, String.ascii_lowercase và String.ascii_uppercase.

    Xem thảo luận

    word1 = st.split["[a-zA-Z]+"]
    
    7
    word1 = st.split["[a-zA-Z]+"]
    
    8
    BuckyBarnes
    5
    BuckyBarnes
    6
    BuckyBarnes
    7
    BuckyBarnes
    8

    word1 = st.split["[a-zA-Z]+"]
    
    7
    word1 = st.split["[a-zA-Z]+"]
    
    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]
    1
    BuckyBarnes
    6
    BuckyBarnes
    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]
    4

    Output:

    initial string :  123[]#$ABGFDabcjw abceddfgh
    first string result:   ABGFDabcjw
    second string result:  abceddfgh

    & nbsp; Phương pháp #2: Sử dụng

    # 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]
    5
    Method #2: Using
    # 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]
    5

    output =  "players year money ipod case mini"
    
    9
    word1 = st.split["[a-zA-Z]+"]
    
    0

    word1 = st.split["[a-zA-Z]+"]
    
    1
    word1 = st.split["[a-zA-Z]+"]
    
    2
    word1 = st.split["[a-zA-Z]+"]
    
    3

    word1 = st.split["[a-zA-Z]+"]
    
    4
    word1 = st.split["[a-zA-Z]+"]
    
    2
    word1 = st.split["[a-zA-Z]+"]
    
    6

    word1 = st.split["[a-zA-Z]+"]
    
    7
    word1 = st.split["[a-zA-Z]+"]
    
    8
    BuckyBarnes
    6
    BuckyBarnes
    7
    BuckyBarnes
    8
    BuckyBarnes
    9

    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]
    1
    word1 = st.split["[a-zA-Z]+"]
    
    2
    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]
    3
    # 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]
    3
    # 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
    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]
    6

    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]
    7
    word1 = st.split["[a-zA-Z]+"]
    
    2
    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]
    3
    # 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]
    3
    # 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
    BuckyBarnes
    2

    word1 = st.split["[a-zA-Z]+"]
    
    7
    word1 = st.split["[a-zA-Z]+"]
    
    8
    BuckyBarnes
    5
    BuckyBarnes
    6
    BuckyBarnes
    7
    BuckyBarnes
    8

    word1 = st.split["[a-zA-Z]+"]
    
    7
    word1 = st.split["[a-zA-Z]+"]
    
    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]
    1
    BuckyBarnes
    6
    BuckyBarnes
    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]
    4

    Output:

    output =  "players year money ipod case mini"
    
    0

    & nbsp; Phương pháp #3: Sử dụng

    output =  "players year money ipod case mini"
    
    2
    Method #3: Using
    output =  "players year money ipod case mini"
    
    2

    word1 = st.split["[a-zA-Z]+"]
    
    1
    word1 = st.split["[a-zA-Z]+"]
    
    2
    word1 = st.split["[a-zA-Z]+"]
    
    3

    word1 = st.split["[a-zA-Z]+"]
    
    4
    word1 = st.split["[a-zA-Z]+"]
    
    2
    word1 = st.split["[a-zA-Z]+"]
    
    6

    word1 = st.split["[a-zA-Z]+"]
    
    7
    word1 = st.split["[a-zA-Z]+"]
    
    8
    BuckyBarnes
    6
    BuckyBarnes
    7
    BuckyBarnes
    8
    BuckyBarnes
    9

    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]
    1
    word1 = st.split["[a-zA-Z]+"]
    
    2
    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]
    3
    # 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]
    3
    # 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
    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]
    6

    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]
    7
    word1 = st.split["[a-zA-Z]+"]
    
    2
    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]
    3
    # 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]
    3
    # 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
    BuckyBarnes
    2

    word1 = st.split["[a-zA-Z]+"]
    
    7
    word1 = st.split["[a-zA-Z]+"]
    
    8
    BuckyBarnes
    5
    BuckyBarnes
    6
    BuckyBarnes
    7
    BuckyBarnes
    8

    word1 = st.split["[a-zA-Z]+"]
    
    7
    word1 = st.split["[a-zA-Z]+"]
    
    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]
    1
    BuckyBarnes
    6
    BuckyBarnes
    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]
    4

    Output:

    output =  "players year money ipod case mini"
    
    1

    Làm cách nào để trích xuất các chữ cái từ một chuỗi?

    Bạn có thể trích xuất một chuỗi con bằng cách chỉ định vị trí và số lượng ký tự hoặc với các mẫu biểu thức chính quy ...
    Trích xuất một chuỗi con bằng cách chỉ định vị trí và số lượng ký tự.....
    Trích xuất một chuỗi con với các biểu thức thông thường: re.search [], re.findall [].
    Ví dụ mẫu biểu thức thông thường ..

    Làm thế nào để bạn chỉ chấp nhận bảng chữ cái trong Python?

    Chỉ cho phép các chữ cái khi lấy đầu vào của người dùng trong Python #..
    Sử dụng vòng lặp thời gian để lặp lại cho đến khi người dùng nhập các chữ cái ..
    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 để tôi có được bảng chữ cái trong Python?

    Cách dễ nhất để tải một danh sách tất cả các chữ cái của bảng chữ cái là sử dụng chuỗi.ascii_letters, String.ascii_lowercase và String.ascii_uppercase.use the string. ascii_letters , string. ascii_lowercase , and string. ascii_uppercase instances.

    Bài Viết Liên Quan

    Chủ Đề