从文本中提取每个单词的第一个字符,并使用函数和 return 方法将其大写

Extract first character of each word from a text and capitalize it with a function and return method

def 缩写(文本): 结果=bla bla bla return 结果

主要

文本=输入("Please enter your text") 首字母(文字)

下面是我们要执行的任务列表:

将句子拆分为单词列表 - 第 1 步
从大写的每个单词中获取第一个字母 - 步骤 2
以这种格式加入他们:{letter}. {another-letter} - step 3
Return 值并打印它 - 步骤 4

我在代码注释中标记了每一步
所以让我们开始吧:

my_word = "stack overflow" # < Change this to any word you'd like
def get_initials(input_word):
  result = input_word.split() # step - 1
  final_result = ""
  for word in result: # loop through our word list
    first_letter = word[0].upper() # step - 2 
    final_result += first_letter + '. ' # step - 3, join
    '''
    So basically get the first letter with word[0]
    and add it to the final_result variable using +=
    and also add an additional ". "(a dot with a space)
    each time, as per requirements
    '''

  return final_result # step - 4, return

print ( get_initials(my_word) ) # and finally step - 4, print

希望这对您有所帮助:)