How to extract all UPPER from a string? Python

后端 未结 7 2129
终归单人心
终归单人心 2020-12-31 04:51
#input
my_string = \'abcdefgABCDEFGHIJKLMNOP\'

how would one extract all the UPPER from a string?

#output
my_upper = \'ABCDEFGHIJKL         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 05:27

    Higher order functions to the rescue!

    filter(str.isupper, "abcdefgABCDEFGHIJKLMNOP")
    

    EDIT: In case you don't know what filter does: filter takes a function and an iterable, and then applies the function to every element in the iterable. It keeps all of the values that return true and throws out all of the rest. Therefore, this will return "ABCDEFGHIJKLMNOP".

提交回复
热议问题