0

I am trying to build a logic where I need to write code in such a way that my string should contain

alphabet and number both. If not print sting does not contain letter else number or both

My code:

stg = 'abc123'

stg.isalnum() 

Output :

True 

Even if my string does not contain number then also gives me : True. That I don't want it

My string should contain at least one letter and number

yyy62103
  • 81
  • 5

2 Answers2

2

you could use

import re
bool(re.match("\d+[a-zA-Z]+|[a-zA-Z]+\d+", stg))
Lucas M. Uriarte
  • 2,403
  • 5
  • 19
0

This will do the work

def hasCharNum(inputString):
    return any(char.isdigit() for char in inputString) and any(char.isalpha() for char in inputString)

hasCharNum("Python3")

True

Casting Spell
  • 31
  • 1
  • 8