简体   繁体   中英

How do I use isalpha() and isspace()

I am trying to return a yes or no answer to show whether the string has spaces or letters. The bolded part is the part I need to get right. I got the other parts...

Here is what I have put and it doesn't work out correctly...

if (string.isalpha() and string.isspace()):
    print('Only alphabetic letters and spaces: yes')
else:
    print('Only alphabetic letters and spaces: no')

How can I use them together? I know how to do this separately but not together. I know this may seem really simple but I am a beginner.

example of the program----

Enter a string: please open my email

Length: 20

First character: p

Last character: l

Contains open: yes

Only alphabetic letters and spaces: yes

Only numeric digits: no

All lower case: yes

All upper case: no

You're testing the whole string. You should be testing each character individually.

is_alpha_or_space = all(c.isalpha() or c.isspace() for c in string)

You can use or and all to check each letter :

if all(char.isalpha() or char.isspace() for char in string):
    print('Only alphabetic letters and spaces: yes')

Two options:

if all(x.isalpha() or x.isspace() for x in original):

or

original.replace(' ','').isalpha()

should work.

您可以尝试将条件更改为str(input.isspace() and input.isalpha())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM