Python, check how much of a string is in uppercase?












2














I have a text, and I want to know if all or a percent bigger than 50% is in uppercase.




DOFLAMINGO WITH TOUCH SCREEN lorem ipsum




I try to use regex(found here a solution):



rx = re.compile(r"^([A-Z ':]+$)", re.M)
upp = rx.findall(string)


But this finds all caps, i don't know if all or more than 50 percent(this includes all) is uppercase ?



I want to number only letters (so no numbers,spaces, new lines etc)










share|improve this question





























    2














    I have a text, and I want to know if all or a percent bigger than 50% is in uppercase.




    DOFLAMINGO WITH TOUCH SCREEN lorem ipsum




    I try to use regex(found here a solution):



    rx = re.compile(r"^([A-Z ':]+$)", re.M)
    upp = rx.findall(string)


    But this finds all caps, i don't know if all or more than 50 percent(this includes all) is uppercase ?



    I want to number only letters (so no numbers,spaces, new lines etc)










    share|improve this question



























      2












      2








      2







      I have a text, and I want to know if all or a percent bigger than 50% is in uppercase.




      DOFLAMINGO WITH TOUCH SCREEN lorem ipsum




      I try to use regex(found here a solution):



      rx = re.compile(r"^([A-Z ':]+$)", re.M)
      upp = rx.findall(string)


      But this finds all caps, i don't know if all or more than 50 percent(this includes all) is uppercase ?



      I want to number only letters (so no numbers,spaces, new lines etc)










      share|improve this question















      I have a text, and I want to know if all or a percent bigger than 50% is in uppercase.




      DOFLAMINGO WITH TOUCH SCREEN lorem ipsum




      I try to use regex(found here a solution):



      rx = re.compile(r"^([A-Z ':]+$)", re.M)
      upp = rx.findall(string)


      But this finds all caps, i don't know if all or more than 50 percent(this includes all) is uppercase ?



      I want to number only letters (so no numbers,spaces, new lines etc)







      python string python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 16:47









      jpp

      92.6k2054103




      92.6k2054103










      asked Nov 21 '18 at 16:28









      user3541631

      1,06821334




      1,06821334
























          7 Answers
          7






          active

          oldest

          votes


















          6














          You can use filter and str.isalpha to clean out non-alphabetic chars and str.isupper to count uppercase chars and calculate the ratio:



          s = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

          alph = list(filter(str.isalpha, s)) # ['D', ..., 'O', 'W', ..., 'N', 'l', 'o', ...]
          sum(map(str.isupper, alph)) / len(alph)
          # 0.7142857142857143


          Also see the docs on sum and map which you might find yourself using regularly. Moreover, this uses the fact that bool is a subclass of int and is cast appropriately for the summation which might be too implicit for the taste of some.






          share|improve this answer































            4














            Regex seems overkill here. You can use sum with a generator expression:



            x = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

            x_chars = ''.join(x.split()) # remove all whitespace
            x_upper = sum(i.isupper() for i in x_chars) > (len(x_chars) / 2)


            Or functionally via map:



            x_upper = sum(map(str.upper, x_chars)) > (len(x_chars) / 2)


            Alternatively, via statistics.mean:



            from statistics import mean

            x_upper = mean(i.isupper() for i in s if not i.isspace()) > 0.5





            share|improve this answer



















            • 2




              Or you could use: statistics.mean(ch.isupper() for ch in s if not ch.isspace())
              – Jon Clements
              Nov 21 '18 at 16:34












            • @JonClements, Yep, good point, but a little messier I think because of the if condition.
              – jpp
              Nov 21 '18 at 16:34








            • 1




              Well, since you're iterating character by character anyway to do the isupper check, I think it's a bit messier to do ''.join(x.split()) to create a new string to iterate over :)
              – Jon Clements
              Nov 21 '18 at 16:43



















            1














            Generic solution that works with any boolean function and iterable (see below for version that only looks at str.isalpha()):



            def percentage(data, boolfunc):
            """Returns how many % of the 'data' returns 'True' for the given boolfunc."""
            return (sum(1 for x in data if boolfunc(x)) / len(data))*100

            text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

            print( percentage( text, str.isupper ))
            print( percentage( text, str.islower ))
            print( percentage( text, str.isdigit ))
            print( percentage( text, lambda x: x == " " ))


            Output:



            62.5  # isupper
            25.0 # islower
            0.0 # isdigit
            12.5 # lambda for spaces




            even better is schwobaseggl's



            return sum(map(boolfunc,data)) / len(data)*100


            because it does not need to persist a list but instead uses a generator.





            Edit: 2nd version that only uses str.isalpha characters and allows multiple boolfuncs:



            def percentage2(data, *boolfuncs):
            """Returns how many % of the 'data' returns 'True' for all given boolfuncs.
            Only uses str.isalpha() characters"""
            return (sum(1 for x in data if all(f(x) for f in boolfuncs)) / sum(
            for x in data if str.isalpha(x)))*100

            text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

            print( percentage2( text, str.isupper, str.isalpha ))
            print( percentage2( text, str.islower, str.isalpha ))


            Output:



            71.42857142857143
            28.57142857142857





            share|improve this answer































              1














              Using regular expressions, this is one way you can do it (given that s is the string in question):



              upper = re.findall(r'[A-Z]', s)
              lower = re.findall(r'[a-z]', s)
              percentage = ( len(upper) / (len(upper) + len(lower)) ) * 100


              It finds the lista of both uppercase and lowercase characters and gets the percentage using their lengths.






              share|improve this answer























              • ( len(upper) / len(upper) + len(lower) ) == 1 + len(lower)
                – Patrick Artner
                Nov 21 '18 at 16:50










              • @Matthieu Brucher, I'll edit and provide some.
                – Pablo Paglilla
                Nov 21 '18 at 17:58






              • 1




                @Patrick Artner, I missed a pair of parenthesis. I'll edit the answer.
                – Pablo Paglilla
                Nov 21 '18 at 18:00



















              0














              Here is one way to do it:



              f = sum(map(lambda c: c.isupper(), f)) / len(f)
              (sum(map(lambda c: c.isupper(), f)) / len(f)) > .50





              share|improve this answer





























                0














                Something like the following should work.



                string = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'
                rx = re.sub('[^A-Z]', '', string)
                print(len(rx)/len(string))





                share|improve this answer





























                  0














                  Try this, it's short and does the job:



                  text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"
                  print("Percent in Capital Letters:", sum(1 for c in text if c.isupper())/len(text)*100)
                  # Percent in Capital Letters: 62.5





                  share|improve this answer





















                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416503%2fpython-check-how-much-of-a-string-is-in-uppercase%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    7 Answers
                    7






                    active

                    oldest

                    votes








                    7 Answers
                    7






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    6














                    You can use filter and str.isalpha to clean out non-alphabetic chars and str.isupper to count uppercase chars and calculate the ratio:



                    s = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

                    alph = list(filter(str.isalpha, s)) # ['D', ..., 'O', 'W', ..., 'N', 'l', 'o', ...]
                    sum(map(str.isupper, alph)) / len(alph)
                    # 0.7142857142857143


                    Also see the docs on sum and map which you might find yourself using regularly. Moreover, this uses the fact that bool is a subclass of int and is cast appropriately for the summation which might be too implicit for the taste of some.






                    share|improve this answer




























                      6














                      You can use filter and str.isalpha to clean out non-alphabetic chars and str.isupper to count uppercase chars and calculate the ratio:



                      s = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

                      alph = list(filter(str.isalpha, s)) # ['D', ..., 'O', 'W', ..., 'N', 'l', 'o', ...]
                      sum(map(str.isupper, alph)) / len(alph)
                      # 0.7142857142857143


                      Also see the docs on sum and map which you might find yourself using regularly. Moreover, this uses the fact that bool is a subclass of int and is cast appropriately for the summation which might be too implicit for the taste of some.






                      share|improve this answer


























                        6












                        6








                        6






                        You can use filter and str.isalpha to clean out non-alphabetic chars and str.isupper to count uppercase chars and calculate the ratio:



                        s = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

                        alph = list(filter(str.isalpha, s)) # ['D', ..., 'O', 'W', ..., 'N', 'l', 'o', ...]
                        sum(map(str.isupper, alph)) / len(alph)
                        # 0.7142857142857143


                        Also see the docs on sum and map which you might find yourself using regularly. Moreover, this uses the fact that bool is a subclass of int and is cast appropriately for the summation which might be too implicit for the taste of some.






                        share|improve this answer














                        You can use filter and str.isalpha to clean out non-alphabetic chars and str.isupper to count uppercase chars and calculate the ratio:



                        s = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

                        alph = list(filter(str.isalpha, s)) # ['D', ..., 'O', 'W', ..., 'N', 'l', 'o', ...]
                        sum(map(str.isupper, alph)) / len(alph)
                        # 0.7142857142857143


                        Also see the docs on sum and map which you might find yourself using regularly. Moreover, this uses the fact that bool is a subclass of int and is cast appropriately for the summation which might be too implicit for the taste of some.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 21 '18 at 16:52

























                        answered Nov 21 '18 at 16:31









                        schwobaseggl

                        36.6k32441




                        36.6k32441

























                            4














                            Regex seems overkill here. You can use sum with a generator expression:



                            x = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

                            x_chars = ''.join(x.split()) # remove all whitespace
                            x_upper = sum(i.isupper() for i in x_chars) > (len(x_chars) / 2)


                            Or functionally via map:



                            x_upper = sum(map(str.upper, x_chars)) > (len(x_chars) / 2)


                            Alternatively, via statistics.mean:



                            from statistics import mean

                            x_upper = mean(i.isupper() for i in s if not i.isspace()) > 0.5





                            share|improve this answer



















                            • 2




                              Or you could use: statistics.mean(ch.isupper() for ch in s if not ch.isspace())
                              – Jon Clements
                              Nov 21 '18 at 16:34












                            • @JonClements, Yep, good point, but a little messier I think because of the if condition.
                              – jpp
                              Nov 21 '18 at 16:34








                            • 1




                              Well, since you're iterating character by character anyway to do the isupper check, I think it's a bit messier to do ''.join(x.split()) to create a new string to iterate over :)
                              – Jon Clements
                              Nov 21 '18 at 16:43
















                            4














                            Regex seems overkill here. You can use sum with a generator expression:



                            x = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

                            x_chars = ''.join(x.split()) # remove all whitespace
                            x_upper = sum(i.isupper() for i in x_chars) > (len(x_chars) / 2)


                            Or functionally via map:



                            x_upper = sum(map(str.upper, x_chars)) > (len(x_chars) / 2)


                            Alternatively, via statistics.mean:



                            from statistics import mean

                            x_upper = mean(i.isupper() for i in s if not i.isspace()) > 0.5





                            share|improve this answer



















                            • 2




                              Or you could use: statistics.mean(ch.isupper() for ch in s if not ch.isspace())
                              – Jon Clements
                              Nov 21 '18 at 16:34












                            • @JonClements, Yep, good point, but a little messier I think because of the if condition.
                              – jpp
                              Nov 21 '18 at 16:34








                            • 1




                              Well, since you're iterating character by character anyway to do the isupper check, I think it's a bit messier to do ''.join(x.split()) to create a new string to iterate over :)
                              – Jon Clements
                              Nov 21 '18 at 16:43














                            4












                            4








                            4






                            Regex seems overkill here. You can use sum with a generator expression:



                            x = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

                            x_chars = ''.join(x.split()) # remove all whitespace
                            x_upper = sum(i.isupper() for i in x_chars) > (len(x_chars) / 2)


                            Or functionally via map:



                            x_upper = sum(map(str.upper, x_chars)) > (len(x_chars) / 2)


                            Alternatively, via statistics.mean:



                            from statistics import mean

                            x_upper = mean(i.isupper() for i in s if not i.isspace()) > 0.5





                            share|improve this answer














                            Regex seems overkill here. You can use sum with a generator expression:



                            x = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'

                            x_chars = ''.join(x.split()) # remove all whitespace
                            x_upper = sum(i.isupper() for i in x_chars) > (len(x_chars) / 2)


                            Or functionally via map:



                            x_upper = sum(map(str.upper, x_chars)) > (len(x_chars) / 2)


                            Alternatively, via statistics.mean:



                            from statistics import mean

                            x_upper = mean(i.isupper() for i in s if not i.isspace()) > 0.5






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 21 '18 at 16:47

























                            answered Nov 21 '18 at 16:30









                            jpp

                            92.6k2054103




                            92.6k2054103








                            • 2




                              Or you could use: statistics.mean(ch.isupper() for ch in s if not ch.isspace())
                              – Jon Clements
                              Nov 21 '18 at 16:34












                            • @JonClements, Yep, good point, but a little messier I think because of the if condition.
                              – jpp
                              Nov 21 '18 at 16:34








                            • 1




                              Well, since you're iterating character by character anyway to do the isupper check, I think it's a bit messier to do ''.join(x.split()) to create a new string to iterate over :)
                              – Jon Clements
                              Nov 21 '18 at 16:43














                            • 2




                              Or you could use: statistics.mean(ch.isupper() for ch in s if not ch.isspace())
                              – Jon Clements
                              Nov 21 '18 at 16:34












                            • @JonClements, Yep, good point, but a little messier I think because of the if condition.
                              – jpp
                              Nov 21 '18 at 16:34








                            • 1




                              Well, since you're iterating character by character anyway to do the isupper check, I think it's a bit messier to do ''.join(x.split()) to create a new string to iterate over :)
                              – Jon Clements
                              Nov 21 '18 at 16:43








                            2




                            2




                            Or you could use: statistics.mean(ch.isupper() for ch in s if not ch.isspace())
                            – Jon Clements
                            Nov 21 '18 at 16:34






                            Or you could use: statistics.mean(ch.isupper() for ch in s if not ch.isspace())
                            – Jon Clements
                            Nov 21 '18 at 16:34














                            @JonClements, Yep, good point, but a little messier I think because of the if condition.
                            – jpp
                            Nov 21 '18 at 16:34






                            @JonClements, Yep, good point, but a little messier I think because of the if condition.
                            – jpp
                            Nov 21 '18 at 16:34






                            1




                            1




                            Well, since you're iterating character by character anyway to do the isupper check, I think it's a bit messier to do ''.join(x.split()) to create a new string to iterate over :)
                            – Jon Clements
                            Nov 21 '18 at 16:43




                            Well, since you're iterating character by character anyway to do the isupper check, I think it's a bit messier to do ''.join(x.split()) to create a new string to iterate over :)
                            – Jon Clements
                            Nov 21 '18 at 16:43











                            1














                            Generic solution that works with any boolean function and iterable (see below for version that only looks at str.isalpha()):



                            def percentage(data, boolfunc):
                            """Returns how many % of the 'data' returns 'True' for the given boolfunc."""
                            return (sum(1 for x in data if boolfunc(x)) / len(data))*100

                            text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

                            print( percentage( text, str.isupper ))
                            print( percentage( text, str.islower ))
                            print( percentage( text, str.isdigit ))
                            print( percentage( text, lambda x: x == " " ))


                            Output:



                            62.5  # isupper
                            25.0 # islower
                            0.0 # isdigit
                            12.5 # lambda for spaces




                            even better is schwobaseggl's



                            return sum(map(boolfunc,data)) / len(data)*100


                            because it does not need to persist a list but instead uses a generator.





                            Edit: 2nd version that only uses str.isalpha characters and allows multiple boolfuncs:



                            def percentage2(data, *boolfuncs):
                            """Returns how many % of the 'data' returns 'True' for all given boolfuncs.
                            Only uses str.isalpha() characters"""
                            return (sum(1 for x in data if all(f(x) for f in boolfuncs)) / sum(
                            for x in data if str.isalpha(x)))*100

                            text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

                            print( percentage2( text, str.isupper, str.isalpha ))
                            print( percentage2( text, str.islower, str.isalpha ))


                            Output:



                            71.42857142857143
                            28.57142857142857





                            share|improve this answer




























                              1














                              Generic solution that works with any boolean function and iterable (see below for version that only looks at str.isalpha()):



                              def percentage(data, boolfunc):
                              """Returns how many % of the 'data' returns 'True' for the given boolfunc."""
                              return (sum(1 for x in data if boolfunc(x)) / len(data))*100

                              text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

                              print( percentage( text, str.isupper ))
                              print( percentage( text, str.islower ))
                              print( percentage( text, str.isdigit ))
                              print( percentage( text, lambda x: x == " " ))


                              Output:



                              62.5  # isupper
                              25.0 # islower
                              0.0 # isdigit
                              12.5 # lambda for spaces




                              even better is schwobaseggl's



                              return sum(map(boolfunc,data)) / len(data)*100


                              because it does not need to persist a list but instead uses a generator.





                              Edit: 2nd version that only uses str.isalpha characters and allows multiple boolfuncs:



                              def percentage2(data, *boolfuncs):
                              """Returns how many % of the 'data' returns 'True' for all given boolfuncs.
                              Only uses str.isalpha() characters"""
                              return (sum(1 for x in data if all(f(x) for f in boolfuncs)) / sum(
                              for x in data if str.isalpha(x)))*100

                              text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

                              print( percentage2( text, str.isupper, str.isalpha ))
                              print( percentage2( text, str.islower, str.isalpha ))


                              Output:



                              71.42857142857143
                              28.57142857142857





                              share|improve this answer


























                                1












                                1








                                1






                                Generic solution that works with any boolean function and iterable (see below for version that only looks at str.isalpha()):



                                def percentage(data, boolfunc):
                                """Returns how many % of the 'data' returns 'True' for the given boolfunc."""
                                return (sum(1 for x in data if boolfunc(x)) / len(data))*100

                                text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

                                print( percentage( text, str.isupper ))
                                print( percentage( text, str.islower ))
                                print( percentage( text, str.isdigit ))
                                print( percentage( text, lambda x: x == " " ))


                                Output:



                                62.5  # isupper
                                25.0 # islower
                                0.0 # isdigit
                                12.5 # lambda for spaces




                                even better is schwobaseggl's



                                return sum(map(boolfunc,data)) / len(data)*100


                                because it does not need to persist a list but instead uses a generator.





                                Edit: 2nd version that only uses str.isalpha characters and allows multiple boolfuncs:



                                def percentage2(data, *boolfuncs):
                                """Returns how many % of the 'data' returns 'True' for all given boolfuncs.
                                Only uses str.isalpha() characters"""
                                return (sum(1 for x in data if all(f(x) for f in boolfuncs)) / sum(
                                for x in data if str.isalpha(x)))*100

                                text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

                                print( percentage2( text, str.isupper, str.isalpha ))
                                print( percentage2( text, str.islower, str.isalpha ))


                                Output:



                                71.42857142857143
                                28.57142857142857





                                share|improve this answer














                                Generic solution that works with any boolean function and iterable (see below for version that only looks at str.isalpha()):



                                def percentage(data, boolfunc):
                                """Returns how many % of the 'data' returns 'True' for the given boolfunc."""
                                return (sum(1 for x in data if boolfunc(x)) / len(data))*100

                                text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

                                print( percentage( text, str.isupper ))
                                print( percentage( text, str.islower ))
                                print( percentage( text, str.isdigit ))
                                print( percentage( text, lambda x: x == " " ))


                                Output:



                                62.5  # isupper
                                25.0 # islower
                                0.0 # isdigit
                                12.5 # lambda for spaces




                                even better is schwobaseggl's



                                return sum(map(boolfunc,data)) / len(data)*100


                                because it does not need to persist a list but instead uses a generator.





                                Edit: 2nd version that only uses str.isalpha characters and allows multiple boolfuncs:



                                def percentage2(data, *boolfuncs):
                                """Returns how many % of the 'data' returns 'True' for all given boolfuncs.
                                Only uses str.isalpha() characters"""
                                return (sum(1 for x in data if all(f(x) for f in boolfuncs)) / sum(
                                for x in data if str.isalpha(x)))*100

                                text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"

                                print( percentage2( text, str.isupper, str.isalpha ))
                                print( percentage2( text, str.islower, str.isalpha ))


                                Output:



                                71.42857142857143
                                28.57142857142857






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 21 '18 at 16:45

























                                answered Nov 21 '18 at 16:32









                                Patrick Artner

                                22k62143




                                22k62143























                                    1














                                    Using regular expressions, this is one way you can do it (given that s is the string in question):



                                    upper = re.findall(r'[A-Z]', s)
                                    lower = re.findall(r'[a-z]', s)
                                    percentage = ( len(upper) / (len(upper) + len(lower)) ) * 100


                                    It finds the lista of both uppercase and lowercase characters and gets the percentage using their lengths.






                                    share|improve this answer























                                    • ( len(upper) / len(upper) + len(lower) ) == 1 + len(lower)
                                      – Patrick Artner
                                      Nov 21 '18 at 16:50










                                    • @Matthieu Brucher, I'll edit and provide some.
                                      – Pablo Paglilla
                                      Nov 21 '18 at 17:58






                                    • 1




                                      @Patrick Artner, I missed a pair of parenthesis. I'll edit the answer.
                                      – Pablo Paglilla
                                      Nov 21 '18 at 18:00
















                                    1














                                    Using regular expressions, this is one way you can do it (given that s is the string in question):



                                    upper = re.findall(r'[A-Z]', s)
                                    lower = re.findall(r'[a-z]', s)
                                    percentage = ( len(upper) / (len(upper) + len(lower)) ) * 100


                                    It finds the lista of both uppercase and lowercase characters and gets the percentage using their lengths.






                                    share|improve this answer























                                    • ( len(upper) / len(upper) + len(lower) ) == 1 + len(lower)
                                      – Patrick Artner
                                      Nov 21 '18 at 16:50










                                    • @Matthieu Brucher, I'll edit and provide some.
                                      – Pablo Paglilla
                                      Nov 21 '18 at 17:58






                                    • 1




                                      @Patrick Artner, I missed a pair of parenthesis. I'll edit the answer.
                                      – Pablo Paglilla
                                      Nov 21 '18 at 18:00














                                    1












                                    1








                                    1






                                    Using regular expressions, this is one way you can do it (given that s is the string in question):



                                    upper = re.findall(r'[A-Z]', s)
                                    lower = re.findall(r'[a-z]', s)
                                    percentage = ( len(upper) / (len(upper) + len(lower)) ) * 100


                                    It finds the lista of both uppercase and lowercase characters and gets the percentage using their lengths.






                                    share|improve this answer














                                    Using regular expressions, this is one way you can do it (given that s is the string in question):



                                    upper = re.findall(r'[A-Z]', s)
                                    lower = re.findall(r'[a-z]', s)
                                    percentage = ( len(upper) / (len(upper) + len(lower)) ) * 100


                                    It finds the lista of both uppercase and lowercase characters and gets the percentage using their lengths.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 21 '18 at 18:02

























                                    answered Nov 21 '18 at 16:35









                                    Pablo Paglilla

                                    29715




                                    29715












                                    • ( len(upper) / len(upper) + len(lower) ) == 1 + len(lower)
                                      – Patrick Artner
                                      Nov 21 '18 at 16:50










                                    • @Matthieu Brucher, I'll edit and provide some.
                                      – Pablo Paglilla
                                      Nov 21 '18 at 17:58






                                    • 1




                                      @Patrick Artner, I missed a pair of parenthesis. I'll edit the answer.
                                      – Pablo Paglilla
                                      Nov 21 '18 at 18:00


















                                    • ( len(upper) / len(upper) + len(lower) ) == 1 + len(lower)
                                      – Patrick Artner
                                      Nov 21 '18 at 16:50










                                    • @Matthieu Brucher, I'll edit and provide some.
                                      – Pablo Paglilla
                                      Nov 21 '18 at 17:58






                                    • 1




                                      @Patrick Artner, I missed a pair of parenthesis. I'll edit the answer.
                                      – Pablo Paglilla
                                      Nov 21 '18 at 18:00
















                                    ( len(upper) / len(upper) + len(lower) ) == 1 + len(lower)
                                    – Patrick Artner
                                    Nov 21 '18 at 16:50




                                    ( len(upper) / len(upper) + len(lower) ) == 1 + len(lower)
                                    – Patrick Artner
                                    Nov 21 '18 at 16:50












                                    @Matthieu Brucher, I'll edit and provide some.
                                    – Pablo Paglilla
                                    Nov 21 '18 at 17:58




                                    @Matthieu Brucher, I'll edit and provide some.
                                    – Pablo Paglilla
                                    Nov 21 '18 at 17:58




                                    1




                                    1




                                    @Patrick Artner, I missed a pair of parenthesis. I'll edit the answer.
                                    – Pablo Paglilla
                                    Nov 21 '18 at 18:00




                                    @Patrick Artner, I missed a pair of parenthesis. I'll edit the answer.
                                    – Pablo Paglilla
                                    Nov 21 '18 at 18:00











                                    0














                                    Here is one way to do it:



                                    f = sum(map(lambda c: c.isupper(), f)) / len(f)
                                    (sum(map(lambda c: c.isupper(), f)) / len(f)) > .50





                                    share|improve this answer


























                                      0














                                      Here is one way to do it:



                                      f = sum(map(lambda c: c.isupper(), f)) / len(f)
                                      (sum(map(lambda c: c.isupper(), f)) / len(f)) > .50





                                      share|improve this answer
























                                        0












                                        0








                                        0






                                        Here is one way to do it:



                                        f = sum(map(lambda c: c.isupper(), f)) / len(f)
                                        (sum(map(lambda c: c.isupper(), f)) / len(f)) > .50





                                        share|improve this answer












                                        Here is one way to do it:



                                        f = sum(map(lambda c: c.isupper(), f)) / len(f)
                                        (sum(map(lambda c: c.isupper(), f)) / len(f)) > .50






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 21 '18 at 16:30









                                        aws_apprentice

                                        2,4061520




                                        2,4061520























                                            0














                                            Something like the following should work.



                                            string = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'
                                            rx = re.sub('[^A-Z]', '', string)
                                            print(len(rx)/len(string))





                                            share|improve this answer


























                                              0














                                              Something like the following should work.



                                              string = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'
                                              rx = re.sub('[^A-Z]', '', string)
                                              print(len(rx)/len(string))





                                              share|improve this answer
























                                                0












                                                0








                                                0






                                                Something like the following should work.



                                                string = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'
                                                rx = re.sub('[^A-Z]', '', string)
                                                print(len(rx)/len(string))





                                                share|improve this answer












                                                Something like the following should work.



                                                string = 'DOFLAMINGO WITH TOUCH SCREEN lorem ipsum'
                                                rx = re.sub('[^A-Z]', '', string)
                                                print(len(rx)/len(string))






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 21 '18 at 16:34









                                                Esteban Quiros

                                                1015




                                                1015























                                                    0














                                                    Try this, it's short and does the job:



                                                    text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"
                                                    print("Percent in Capital Letters:", sum(1 for c in text if c.isupper())/len(text)*100)
                                                    # Percent in Capital Letters: 62.5





                                                    share|improve this answer


























                                                      0














                                                      Try this, it's short and does the job:



                                                      text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"
                                                      print("Percent in Capital Letters:", sum(1 for c in text if c.isupper())/len(text)*100)
                                                      # Percent in Capital Letters: 62.5





                                                      share|improve this answer
























                                                        0












                                                        0








                                                        0






                                                        Try this, it's short and does the job:



                                                        text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"
                                                        print("Percent in Capital Letters:", sum(1 for c in text if c.isupper())/len(text)*100)
                                                        # Percent in Capital Letters: 62.5





                                                        share|improve this answer












                                                        Try this, it's short and does the job:



                                                        text = "DOFLAMINGO WITH TOUCH SCREEN lorem ipsum"
                                                        print("Percent in Capital Letters:", sum(1 for c in text if c.isupper())/len(text)*100)
                                                        # Percent in Capital Letters: 62.5






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Nov 21 '18 at 16:42









                                                        Manrique

                                                        498112




                                                        498112






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.





                                                            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                            Please pay close attention to the following guidance:


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416503%2fpython-check-how-much-of-a-string-is-in-uppercase%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            404 Error Contact Form 7 ajax form submitting

                                                            How to know if a Active Directory user can login interactively

                                                            TypeError: fit_transform() missing 1 required positional argument: 'X'