【问题标题】:Extract first character of each word from the sentence string [closed]从句子字符串中提取每个单词的第一个字符[关闭]
【发布时间】:2016-12-16 19:41:52
【问题描述】:

我需要编写一个代码,当您输入文本时,它会从您放置的句子中的每个单词中提取第一个字母。

例如,对于示例字符串"I like to play guitar and piano and drums",它应该打印"Iltpgapad"

【问题讨论】:

标签: python string


【解决方案1】:

试试这样的:

line = "I like to play guitar and piano and drums"
words = line.split()
letters = [word[0] for word in words]
print "".join(letters)

【讨论】:

    【解决方案2】:

    此 sn-p 与 Python 3.x 兼容:

    print(''.join([x[0] for x in raw_input("Enter text:").split()]))
    

    【讨论】:

      【解决方案3】:
      line = "I like to play guitar and piano and drums"
      letters = ""
      words = line.split()
      for word in words:
          letters = letters + word[0]
      print(" ".join(letters).upper())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-08
        • 2013-08-24
        • 2012-03-21
        • 2021-12-06
        • 2018-10-24
        • 1970-01-01
        相关资源
        最近更新 更多