Python Forum
check if value of passed variable has uppercase characters in it. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: check if value of passed variable has uppercase characters in it. (/thread-4659.html)



check if value of passed variable has uppercase characters in it. - wfsteadman - Sep-01-2017

Greetings all,
I am parsing through lines in a file and what i am trying to do is look for any objects in position 5 (The line is getting split so it would be line[5]) and if line[5]contains an uppercase character anywhere in the name, I will then pass it to another portion of my script that will change the address to all lower case. So in the example below, line one would need to be changed
from: config firewall address trust edit "ADDRESS-216" set subnet 10.XX.181.163 255.255.255.255
to: config firewall address trust edit "address-216" set subnet 10.XX.181.163 255.255.255.255

below is a sample of the lines that are passed to the for loop:
config firewall address trust edit "ADDRESS-216" set subnet 10.XX.181.163 255.255.255.255
config firewall address trust edit "address-217" set subnet 10.XX.187.27 255.255.255.255
config firewall address trust edit "addRESS-218" set subnet 10.XX.69.163 255.255.255.255
config firewall address trust edit "ADDRess-219" set subnet 10.XX.75.27 255.255.255.255
config firewall address trust edit "ADDRESS" set subnet 10.XX.75.27 255.255.255.255
config firewall address trust edit "address" set subnet 10.XX.75.27 255.255.255.255

Below is a test where I have tried just using a list and doing a for loop to try to test changing the case with the following results. So I am not sure that isupper and isalpha work for my needs. But not sure how to get where I need to go.

listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368']

for test in listname:
    if test == test.isupper() and test.isalpha():
        print(test.lower())
    else:
        print(test + "is good")
        
and the output I am getting is:
ONE is good
two is good
Thr33 is good
four is good
FiVe368 is good

what I would want to see is
one
two is good
three
four is good
five368


RE: check if value of passed variable has uppercase characters in it. - hbknjr - Sep-01-2017

In Line 4: if test == test.isupper()
test = String
test.isupper() = Boolean

So, each time Python compares (string == Boolean) and False is returned. Therefore it jumps to else statement.

Here's the correct code.
listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368']
 
for test in listname:
    if test.isupper() and test.isalpha(): 
        print(test.lower())
    else:
        print(test + "is good")



RE: check if value of passed variable has uppercase characters in it. - wfsteadman - Sep-01-2017

I think I figured it out since writing this:

import re
pattern = re.compile("([A-Z])")
listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368']

for test in listname:
    if pattern.match(test):
        print(test.lower())
    else:
        print(test + " is good")
The output is:
one
two is good
thr33
four is good
five368
[Finished in 0.2s]


RE: check if value of passed variable has uppercase characters in it. - metulburr - Sep-01-2017

you shouldnt need to use regex for simple string checks.

Because str.lower() does not effect string'd digits, you can just check for upper
listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368']
  
for name in listname:
    if any(char.isupper() for char in name):
        print(name.lower())
    else:
        print("{} is good".format(name))
Output:
one two is good thr33 four is good five368
If you really had to check digit you could do that as well
listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368', '67', '245245']
  
for name in listname:
    if any(char.isdigit() or char.isupper() for char in name):
        print(name.lower())
    else:
        print("{} is good".format(name))
Output:
one two is good thr33 four is good five368 67 245245