0

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 by dan.nitschke: Forgot

7
Contributors
12
Replies
14
Views
4 Years
Discussion Span
Last Post by vegaseat
Featured Replies
  • 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") Read More

  • 3

    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 … Read More

  • 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 Read More

  • 1

    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 … Read More

  • 2

    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 = … Read More

0

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!

2

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")
Votes + Comments
well done
0

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 by vegaseat

3

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
1

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
1

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 by pyTony

Votes + Comments
nice!
0

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

Edited by woooee

0

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 by Ene Uran

1

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
2

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.