[Tutor] uplownumpnct.py

Mats Wichmann mats at wichmann.us
Mon Jan 1 15:04:54 EST 2024


On 1/1/24 10:22, Ethan Rosenberg wrote:
> Tutor -
> 
> What is my syntax error?
> 
> #uplownumpnct.py
> #To determine uppercase, lowercase, punctuation and special characters in a
> string
> 
> 
> up=
> ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
> numm = [1,2,3,4,5,6,7,8,9,0]
> pnct = [:,;.,,]
> spec = [!,@,<,#,>,},$,%,&,?,*,(,),{,/,' ']
> 
> sent = 'THIS is the way we wash 1 2 3 , , !'
> list2 = list(sent)
> lgn = len(sent)
> lgn2 = lgn
> print(lgn)
> 
> for i in range(0,lgn-1):
> {
>          if list2[i] in up:
>              upp+=
> 
>         if list2[i] in  low:
>              lww+=
> 
>         if list2[i]  in numm:
>              numnum+=
> 
>         if list2[i] in pnct:
>              numpct+=
> 
>         if list2[i] in spec:
>              numspec+=
> 
> }
> 
>       prnt
> 
> def prnt:
>       print('Num uppercase  ', upp)
>       print('Num lowercase  ',lww)
>       print('Num  numbers ',  numnum)
>       print('Num  punctuation  ', numpnct)
>       print('Num  special characters' , numspec)
>             #uplownumpnct.py
> #To determine uppercase, lowercase, punctuation and special characters in a
> string
> 
> 
> up=
> ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
> numm = [1,2,3,4,5,6,7,8,9,0]
> pnct = [:,;.,,]
> spec = [!,@,<,#,>,},$,%,&,?,*,(,),{,/,' ']

in fact, you can also get uppercase, etc. from the string module.

 >>> import string
 >>> string.digits
'0123456789'
 >>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
 >>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 >>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
 >>> string.whitespace
' \t\n\r\x0b\x0c'
 >>>

> sent = 'THIS is the way we wash 1 2 3 , , !'
> list2 = list(sent)
> lgn = len(sent)

you don't need any of that

> lgn2 = lgn
> print(lgn)
> 
> for i in range(0,lgn-1):
> {

That is C/javascript/etc. syntax.  Python delimits a code block 
following a : using required indentation, no braces involved. Or allowed.

And... you can (and should) just iterate over the string directly, like:

for c in sent:
     if c in up:
         upp += 1

Indeed, Python can also make that check for you using a string method:

     if c.isupper():
         upp += 1

there's islower() and isdigit() and others.



More information about the Tutor mailing list