Hướng dẫn how to check if a string contains only alphabets and space in python - cách kiểm tra xem một chuỗi chỉ chứa các bảng chữ cái và dấu cách trong python

Giải pháp chính xác sẽ sử dụng or.

string = input["Enter a string: "]

if all[x.isalpha[] or x.isspace[] for x in string]:
    print["Only alphabetical letters and spaces: yes"]
else:
    print["Only alphabetical letters and spaces: no"]

Mặc dù bạn có một chuỗi, bạn đang lặp lại các chữ cái của chuỗi đó, vì vậy bạn có một chữ cái cùng một lúc. Vì vậy, một mình char không thể là một nhân vật bảng chữ cái và một không gian vào thời điểm đó, nhưng nó sẽ chỉ cần là một trong hai để thỏa mãn ràng buộc của bạn.

Chỉnh sửa: Tôi đã thấy nhận xét của bạn trong câu trả lời khác. alphabet = string.isalpha[] Trả về True, nếu và chỉ khi tất cả các ký tự trong chuỗi là chữ cái theo thứ tự chữ cái. Đây không phải là những gì bạn muốn, bởi vì bạn đã nói rằng bạn muốn mã của mình in yes khi thực thi với chuỗi ____10, có không gian. Bạn cần phải tự kiểm tra từng chữ cái, không phải toàn bộ chuỗi. I saw your comment in the other answer. alphabet = string.isalpha[] return True, if and only if all characters in a string are alphabetical letters. This is not what you want, because you stated that you want your code to print yes when execute with the string

>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
    print["Only alphabetical letters and spaces: yes"]
else:
    print["Only alphabetical letters and spaces: no"]


Only alphabetical letters and spaces: yes
0, which has a space. You need to check each letter on its own, not the whole string.

Chỉ cần thuyết phục bạn rằng mã thực sự là chính xác [tốt, ok, bạn cần tự thực hiện nó để bị thuyết phục, nhưng dù sao]

>>> string = "please "
>>> if all[x.isalpha[] or x.isspace[] for x in string]:
    print["Only alphabetical letters and spaces: yes"]
else:
    print["Only alphabetical letters and spaces: no"]


Only alphabetical letters and spaces: yes

EDIT 2: Đánh giá từ nhận xét mới của bạn, bạn cần một cái gì đó như thế này: Judging from your new comments, you need something like this:

def hasSpaceAndAlpha[string]:
    return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]

>>> hasSpaceAndAlpha["text# "]
False
>>> hasSpaceAndAlpha["text"]
False
>>> hasSpaceAndAlpha["text "]
True

hoặc

def hasSpaceAndAlpha[string]:
    if any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]

>>> hasSpaceAndAlpha["text# "]
Only alphabetical letters and spaces: no
>>> hasSpaceAndAlpha["text"]
Only alphabetical letters and spaces: no
>>> hasSpaceAndAlpha["text "]
Only alphabetical letters and spaces: yes

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận 
    This is one of the way in which this task can be performed. In this, we compare the string for all elements being alphabets or space only.

    Python3

    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    1
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    2

    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    3
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    4
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    5

    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    6
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    7
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    8
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    9
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    0

    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    1
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    4
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    3
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    4
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    5
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    6
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    7
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    7__

    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    6
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    7
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    0
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    9
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    2
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    3

    Đầu ra: & nbsp;

    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True

    Phương pháp số 1: Sử dụng Regex & NBSP; vấn đề này cũng có thể được giải quyết bằng cách sử dụng Regex để chỉ bao gồm không gian và bảng chữ cái trong một chuỗi. 
    This problem can also be solved by employing regex to include only space and alphabets in a string.

    Python3

    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    1
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    2

    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    3
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    4
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    5

    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    6
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    7
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    8
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    9
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    0

    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    1
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    4
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    3
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    4
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    5
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    6
    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    7
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    7__

    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    6
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    7
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    0
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    9
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    2
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    3

    Đầu ra: & nbsp;

    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True

    Phương pháp số 1: Sử dụng Regex & NBSP; vấn đề này cũng có thể được giải quyết bằng cách sử dụng Regex để chỉ bao gồm không gian và bảng chữ cái trong một chuỗi.

    def hasSpaceAndAlpha[string]:
        return any[char.isalpha[] for char in string] and any[char.isspace[] for char in string] and all[char.isalpha[] or char.isspace[] for char in string]
    
    >>> hasSpaceAndAlpha["text# "]
    False
    >>> hasSpaceAndAlpha["text"]
    False
    >>> hasSpaceAndAlpha["text "]
    True
    
    1
    >>> string = "please "
    >>> if all[x.isalpha[] or x.isspace[] for x in string]:
        print["Only alphabetical letters and spaces: yes"]
    else:
        print["Only alphabetical letters and spaces: no"]
    
    
    Only alphabetical letters and spaces: yes
    
    4
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    6
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    7
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    8
    The original string is : geeksforgeeks is best for geeks
    Does String contain only space and alphabets : True
    9
    O[n]

    Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:O[n]


    Làm thế nào để bạn kiểm tra xem một chuỗi chỉ chứa các chữ cái và khoảng trống trong Python?

    Phương thức số 1: Sử dụng tất cả [] + ISSPace [] + isalpha [] Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi so sánh chuỗi cho tất cả các yếu tố chỉ là bảng chữ cái hoặc không gian.Using all[] + isspace[] + isalpha[] This is one of the way in which this task can be performed. In this, we compare the string for all elements being alphabets or space only.

    Làm thế nào để bạn kiểm tra xem một chuỗi chỉ có chữ cái và khoảng trống?

    Chúng ta cũng có thể sử dụng phương thức chuỗi khớp [] để kiểm tra xem một chuỗi chỉ chứa các chữ cái và khoảng cách.Phương thức Trận đấu chuỗi [] trả về một mảng của tất cả các kết quả phù hợp của biểu thức chính quy trong một chuỗi.Nếu không có trận đấu, nó sẽ trả về null.use the String match[] method to check if a string contains only letters and spaces. The String match[] method returns an array of all the matches of a regular expression in a string. If there are no matches, it returns null .

    Làm cách nào để kiểm tra xem một chuỗi chứa tất cả các chữ cái của bảng chữ cái trong Python?

    Để kiểm tra xem một chuỗi chỉ chứa bảng chữ cái, hãy sử dụng hàm isalpha [] trên chuỗi.Isalpha [] trả về giá trị boolean.Giá trị trả về là đúng nếu chuỗi chỉ chứa bảng chữ cái và sai nếu không.use the function isalpha[] on the string. isalpha[] returns boolean value. The return value is True if the string contains only alphabets and False if not.

    Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một khoảng trống trong Python không?

    Phương thức Python String ISSPACE [].Phương thức Python String ISSPACE [] trả về True True nếu tất cả các ký tự trong chuỗi là ký tự khoảng trắng, nếu không, nó sẽ trả về Sai False.Hàm này được sử dụng để kiểm tra xem đối số có chứa tất cả các ký tự khoảng trắng không, chẳng hạn như: '' - không gian.. Python String isspace[] method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as: ' ' – Space.

    Chủ Đề