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

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")
commented: well done +14

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)

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
commented: nice! +9

pyTony's code does not work on my US computer, it gives False for his name.

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

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?)

Just a note, Python3 does not contain string.letters

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.