Hello,

I am trying to make a variable (name) so it only accepts alphabet characters (eg. DaNiEl) not $@@4234.

Below is an example of the code i'm trying to use. I have tried a few functions, to no avail.

Can someone please post the answer (in code form). Much Appreciated.

 name = str(input("What is the customers name? "))
    print("")
    while True:
        @@@WHAT FUNCTION GOES HERE THAT DENIES INVALID TEXT?@@@@@
            print("")
            print("You've typed something wrong, please only input letters.")
            name = str(input("What is the customers name? "))
        else:
            break

Edited 4 Years Ago by dan.nitschke: Forgot

Try:

name.strip().isalpha();

.strip() will remove spaces above and at the end of the string, .isalpha() will return true/false, you will get false if there is a space in the middle, to avoid that you can add .replace:

name.strip().replace(' ','',1).isalpha();

bye!

Maybe this will help you:

import string

allowed_alpha = string.ascii_letters + string.whitespace

# a test name
name = "Mark Zumkoff"

# gives False because of space
print(name.isalpha())

# this test allows spaces
if all(c in allowed_alpha for c in name):
    print(name)
else:
    print("not an allowed name")
Comments
well done

Looks at the problem from another direction ...

name = "Mark Zumkoff!"

# testing only
# lists all characters that are not alpha or space
print([c for c in name if not (c.isalpha() or c.isspace())])

if len([c for c in name if not (c.isalpha() or c.isspace())]):
    print("not an allowed name")
else: 
    print(name)

Edited 4 Years Ago by vegaseat

Pack that into a function and add some safe-guards ...

def is_alpha_space(name):
    """
    return True if all characters in name are alpha or white space
    """
    # strip off any leading/trailing white spaces
    name = name.strip()
    if len(name) < 1:
        return False
    return not len([c for c in name if not (c.isalpha() or c.isspace())])

name = "Mark Zumkoff"
print(is_alpha_space(name))  # True

name = "Mark Zumkoff!"
print(is_alpha_space(name))  # False

name = "    "
print(is_alpha_space(name))  # False

A regex version,but i do like Vega soultion better.

import re

def is_alpha_space(name):
    name_nospace = ''.join((name.split()))
    if re.search(r"\W", name_nospace) or len(name_nospace) < 1:
        return False
    return True

name = "Mark Zumkoff"
print(is_alpha_space(name))  # True

name = "Mark Zumkoff@"
print(is_alpha_space(name))  # False

name = "    "
print(is_alpha_space(name))  # False

You can also use strip (which is very fast, fastest pandigital check is for example by using lengths and strip('012356789'))

# -*- coding: latin1 -*-
from string import letters

def is_alpha_space(name):
    return False if not name.strip() else not name.strip(letters+' ')

name = "Väinö Veijalainen"
print(is_alpha_space(name))  # True

name = "Mark Zumkoff@"
print(is_alpha_space(name))  # False

name = "    "
print(is_alpha_space(name))  # False

Edited 4 Years Ago by pyTony

Comments
nice!

It depends on what is in "letters". If your OS is set to ASCII then the latin1 characters will not be found.

Edited 4 Years Ago by woooee

Now I have a few questions to out international friends.

This is what I get from my computer here in the USA:

# run on a computer set to English language

import string

eng_letters = string.letters

print(eng_letters)

'''my result -->
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
'''

Here are my questions:

What does string.letters look like in your country?

Does isalpha() include the alphabet of your country?

Could you post the results of string.letters so we can use that constant?
(Maybe they are published somewhere?)

Edited 4 Years Ago by Ene Uran

So better then to use compatible with both:

# -*- coding: latin1 -*-
def is_alpha_space(name):
    return (any(c.isalpha() for c in name) and
            all(c.isalpha() or c == ' ' for c in name))

name = "Väinö Veijalainen"
print(is_alpha_space(name))  # True

name = "Mark Zumkoff@"
print(is_alpha_space(name))  # False

name = "    "
print(is_alpha_space(name))  # False

Hmmmm ...

# -*- coding: latin1 -*-

# coding adds proper letters to isalpha() with Python 3.2
# but not Python 2.7

def is_alpha_space(name):
    return (any(c.isalpha() for c in name) and
            all(c.isalpha() or c == ' ' for c in name))

name = "Väinö Veijalainen"
print(is_alpha_space(name))  # True

name = "Mark Zumkoff@"
print(is_alpha_space(name))  # False

name = "    "
print(is_alpha_space(name))  # False
This question has already been answered. Start a new discussion instead.