Here is the answer to your question:
Interpreting what you want _(not -), this should complete the task:
>>> tests = ["a", "A", "a1", "a_1", "1a", "_a", "a\n", "", "z_"]
>>> for test in tests:
... print repr(test), bool(re.match(r"[A-Za-z]\w*\Z", test))
...
'a' True
'A' True
'a1' True
'a_1' True
'1a' False
'_a' False
'a\n' False
'' False
'z_' True
>>>
$; :
, , $ , \Z
>>> re.match(r"[a-zA-Z][\w-]*$","A")
<_sre.SRE_Match object at 0x00BAFE90>
>>> re.match(r"[a-zA-Z][\w-]*$","A\n")
<_sre.SRE_Match object at 0x00BAFF70>
>>>
>>> re.match(r"[a-zA-Z][\w-]*\Z","A")
<_sre.SRE_Match object at 0x00BAFE90>
>>> re.match(r"[a-zA-Z][\w-]*\Z","A\n")
>>>
Fine Manual :
'$'
โโ [ ], MULTILINE . foo "foo" "foobar", foo $ "foo". , foo. $ 'foo1\nfoo2\n' 'foo2 , ' foo1 MULTILINE; $ 'foo\n' () : .
\ Z
.
=== - ===
>>> import string
>>> letters = set(string.ascii_letters)
>>> ok_chars = letters | set(string.digits + "_")
>>>
>>> def is_valid_name(strg):
... return strg and strg[0] in letters and all(c in ok_chars for c in strg)
...
>>> for test in tests:
... print repr(test), repr(is_valid_name(test))
...
'a' True
'A' True
'a1' True
'a_1' True
'1a' False
'_a' False
'a\n' False
'' ''
'z_' True
>>>