Upper case first letter of each word in a phrase

前端 未结 10 1523
栀梦
栀梦 2020-12-03 21:08

Ok, I\'m trying to figure out how to make a inputed phrase such as this in python ....

Self contained underwater breathing apparatus

output

相关标签:
10条回答
  • 2020-12-03 21:59

    Pythonic Idioms

    • Using a generator expression over str.split()
    • Optimize the inner loop by moving upper() to one call at outside of the loop.

    Implementation:

    input = 'Self contained underwater breathing apparatus'
    output = ''.join(word[0] for word in input.split()).upper()
    
    0 讨论(0)
  • 2020-12-03 22:08
    def myfunction(string):
        return (''.join(map(str, [s[0] for s in string.upper().split()])))
    
    myfunction("Self contained underwater breathing apparatus")
    

    Returns SCUBA

    0 讨论(0)
  • 2020-12-03 22:10
    s = "Self contained underwater breathing apparatus" 
    for item in s.split():
        print item[0].upper()
    
    0 讨论(0)
  • 2020-12-03 22:11

    I believe you can get it done this way as well.

    def get_first_letters(s):
        return ''.join(map(lambda x:x[0].upper(),s.split()))
    
    0 讨论(0)
提交回复
热议问题