Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isalpha python function won't consider spaces

So the code below takes an input and makes sure the input consists of letters and not numbers. How would i make it also print orginal if the input contains a space

original = raw_input("Type the name of the application: ")

if original.isalpha() and len(original) > 0:
    print original
else:
    print "empty"

tried this code but worked when the input was a number too.

original = raw_input("Type the word you want to change: ")

if original.isalpha() or len(original) > 0:
    print original
else:
    print "empty"
like image 383
Ragnar Avatar asked Jan 02 '14 19:01

Ragnar


People also ask

Does Isalpha count spaces?

isalpha python function won't consider spaces.

How do you allow spaces in Python?

We add space in string in python by using rjust(), ljust(), center() method. To add space between variables in python we can use print() and list the variables separate them by using a comma or by using the format() function.

Does Isalnum include spaces?

Python String isalnum() Method The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9). Example of characters that are not alphanumeric: (space)!

How do you check if a string contains only alphabets and spaces in 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.


1 Answers

It looks like that's just how string works.

Two options:

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

(modified on inspectorG4dget's recommendation below)

or

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

should work.

like image 144
Corley Brigman Avatar answered Oct 05 '22 22:10

Corley Brigman