How do you check if a string contains only alphabets and spaces in python?

The correct solution would use an 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"]

Although you have a string, you are iterating over the letters of that string, so you have one letter at a time. So, a char alone cannot be an alphabetical character AND a space at the time, but it will just need to be one of the two to satisfy your constraint.

EDIT: 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 please, which has a space. You need to check each letter on its own, not the whole string.

Just to convince you that the code is indeed correct [well, ok, you need to execute it yourself to be convinced, but anyway]:

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

or

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

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while testing of credibility of string being a part of containing just alphabets, an exception of spaces has to be mentioned explicitly and becomes a problem. This can occur in domains that deal with data. Lets discuss certain ways in which this task can be performed.

    Method #1 : 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.

    Python3

    import re

    test_str = 'geeksforgeeks is best for geeks'

    print["The original string is : " + test_str]

    res = test_str != '' and all[chr.isalpha[] or chr.isspace[] for chr in test_str]

    print["Does String contain only space and alphabets : " + str[res]]

    Output : 

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

    Method #1 : Using regex 
    This problem can also be solved by employing regex to include only space and alphabets in a string.

    Python3

    import re

    test_str = 'geeksforgeeks is best for geeks'

    print["The original string is : " + test_str]

    res = bool[re.match['[a-zA-Z\s]+$', test_str]]

    print["Does String contain only space and alphabets : " + str[res]]

    Output : 

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

    The Time and Space Complexity for all the methods are the same:

    Time Complexity: O[n]

    Auxiliary Space: O[n]


    How do you check if a string only contains letters and spaces Python?

    Method #1 : 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.

    How do you know if a string is only letters and spaces?

    The RegExp test[] Method To check if a string contains only letters and spaces in JavaScript, call the test[] method on this regex: /^[A-Za-z\s]*$/ . If the string contains only letters and spaces, this method returns true . Otherwise, it returns false .

    How do you check if a string contains all letters Python?

    Using Python's "in" operator The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .

    How do you check if a string is just spaces Python?

    Python isspace[] method is used to check space in the string. It returna true if there are only whitespace characters in the string. Otherwise it returns false. Space, newline, and tabs etc are known as whitespace characters and are defined in the Unicode character database as Other or Separator.

    Chủ Đề