如何在最后一个字母后停止打印点?

时间:2013-10-28 17:58:13

标签: python generator

printmystring = ""

mystring = input("Enter the sentence you wish to be an acronym: ")

mystring_length = len(mystring)

for i in range (0,mystring_length):
    if  mystring[i].isupper():
        print(mystring[i] ,end="")
        print(".",end="")

我的输入是“为了您的信息”

输出为F.Y.I。

4 个答案:

答案 0 :(得分:2)

对于如此小的任务,代码似乎相当复杂。

让我提出一个不同的解决方案,我们收集列表中的所有大写字母。

mystring = input("Enter the sentence you wish to be an acronym: ")
upper_letters = [letter for letter in mystring if letter.isupper()]
print('.'.join(upper_letters))

moarningsun提出了另一种方法:

upper_letters = filter(str.isupper, mystring)

答案 1 :(得分:0)

我很确定这会奏效:

mystring = input("Enter the sentence you wish to be an acronym: ")

mystring_length = len(mystring)

print(mystring[0] ,end="")

for i in range (1,mystring_length):
    if mystring[i].isupper():
        print(". " ,end="") 
        print(mystring[i],end="")

答案 2 :(得分:0)

你正在寻找这样的东西:

#initialize variables
buffer = ""
words = list()

#get input
mystring = input("Enter the sentence you wish to be an acronym: ")
mystring_length = len(mystring)

#parse words
for i in range (0, mystring_length)
     if mystring[i] == " "
         words.append(buffer)
         buffer = ""
     else
         buffer += mystring[i]

#add the final word to buffer
#as it is not followed by a space, it will not be added using the above algorithm
words.append(buffer)
buffer = ""

#loop through words
for i in range (0, len(words))
     print(words[i], end="")
     if (i != len(words))
          print(".", end="")

编辑为python(我认为)

答案 3 :(得分:0)

def acronym(sentence):
    return '.'.join([x[0].upper() for x in sentence.split()])