How to extract all UPPER from a string? Python

后端 未结 7 2065
终归单人心
终归单人心 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:40
    import string
    s = 'abcdefgABCDEFGHIJKLMNOP'
    s.translate(None,string.ascii_lowercase)
    

    string.translate(s, table[, deletechars]) function will delete all characters from the string that are in deletechars, a list of characters. Then, the string will be translated using table (we are not using it in this case).

    To remove only the lower case letters, you need to pass string.ascii_lowercase as the list of letters to be deleted.

    The table is None because when the table is None, only the character deletion step will be performed.

    0 讨论(0)
提交回复
热议问题