如何创建一个脚本,从Python 3中的输入创建首字母缩略词

时间:2017-03-16 21:39:03

标签: python-3.x

我找到了这个问题的另一个解决方案,但它使用的是Python 2.7

import re

words = "internet explorer 10"
print re.sub(r"([a-zA-Z])[a-z,A-Z]+\s*",r"\1",words).upper()

^^这是他们用于Python 2.7的解决方案,我编辑它看起来更像是这样:

import re

words = input()
print re.sub(r"([a-zA-Z])[a-z,A-Z]+\s*",r"\1",words).upper()

我希望能够将用户的输入变成首字母缩略词,抓住所有首字母来制作它。

2 个答案:

答案 0 :(得分:0)

import re
words = input()
print(re.sub(r"([a-zA-Z])[a-z,A-Z]+\s*",r"\1", words).upper())

答案 1 :(得分:0)

这是一个想法:

# creates acronyms

text = input("Enter your words to 'acronymize' \n" + ">>> ")
acronym = ""

textArr = text.split(" ")

for word in textArr:
    if not word.startswith(("1", "2", "3", "4", "5", "6", "8", "9", "0")):
        letter = word[0]
        acronym += letter.upper()
    elif word.startswith(("1", "2", "3", "4", "5", "6", "8", "9", "0")):
        acronym += " " + word + " "

print(acronym)
相关问题