Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to count uppercase, lowercase, special characters, and numeric values using RegEx in Python

1 Answer

0 votes
import re

def count_characters(text):
    uppercase = len(re.findall(r'[A-Z]', text))
    lowercase = len(re.findall(r'[a-z]', text))
    digits    = len(re.findall(r'\d', text))
    special   = len(re.findall(r'[^A-Za-z0-9]', text))

    return uppercase, lowercase, digits, special


s = "Python@2026!"

u, l, d, spc = count_characters(s)

print("Uppercase:", u)
print("Lowercase:", l)
print("Digits:", d)
print("Special characters:", spc)



'''
run:

Uppercase: 1
Lowercase: 5
Digits: 4
Special characters: 2

'''

 



answered 5 hours ago by avibootz
...