-1

Let's say I have string:

string1 = "h \\\\\ 2*2"

How do I check the string to make sure it doesn't have any letters, regardless of capitlization.

So like

I have

if "a" in string1 or "A" in string1:
    print("it's got an A bro")
elif "b" in string1 or "B" in string1:
    print("it's got a B bro")
#cont...
else:
    print("There no letters in ur string bro")

There's gotta be a faster way to do this I assume. I am only looking for letters. Everything else can stay. like "\" or "*" those are good.

  • Does this answer your question? [How can I check if character in a string is a letter? (Python)](https://stackoverflow.com/questions/15558392/how-can-i-check-if-character-in-a-string-is-a-letter-python) – AMC Mar 09 '20 at 01:31
  • @AMC yes thank you very much love ya babe. –  Mar 09 '20 at 01:33

1 Answers1

0

A regex search is probably the simplest way:

string1 = "h      \\\\\ 2*2"
if not re.search(r'^.*[A-Za-z].*$', string1):
    print("MATCH")
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • wow that's fancy. i've never seen something so cool like that it's what I've always dreamed of when coding –  Mar 09 '20 at 01:33
  • @Dr.SuessOfficial Not really `:-)` ... even the cat in the hat could figure it out. – Tim Biegeleisen Mar 09 '20 at 01:34
  • maybe one day hopefully –  Mar 09 '20 at 01:34
  • _A regex search is probably the simplest way_ Wouldn’t a loop with `str.isalpha()` be simpler, and more versatile? – AMC Mar 09 '20 at 01:44
  • @AMC Performance should be somewhat similar, and I would much rather maintain a single clean one-liner than a loop, wouldn't you? – Tim Biegeleisen Mar 09 '20 at 01:47
  • @TimBiegeleisen _I would much rather maintain a single clean one-liner than a loop, wouldn’t you_ Only a single one? Sure, maybe. What happens when there’s more than one, though? Also, did you intend to use `re.match()` or `re.fullmatch()` instead of `re.search()` ? If you’re sticking with the latter option, why not just use `not re.search(r"[A-z]", string1)` ? Wouldn’t the effect be the same? – AMC Mar 09 '20 at 02:06
  • Never mind the fact, of course, that both regex solutions fail on characters such as `é` and `à`, which can hardly be described as rare. – AMC Mar 09 '20 at 02:20