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"
isalpha python function won't consider spaces.
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.
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)!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With