给一个名字,打印名字的首字母(大写)和姓氏(第一个字母大写),用点隔开。
null
例如:
Input : geeks for geeks Output : G.F.Geeks Input : mohandas karamchand gandhi Output : M.K.Gandhi
A. 幼稚的方法 其中之一就是重复空格,并在每个空格后打印下一个字母,最后一个空格除外。在最后一个空格中,我们必须用一种简单的方法将所有字符放在最后一个空格之后。
使用 内置函数中的Python 我们可以 分裂 将单词排成一个列表,然后遍历到最后一个单词,并使用大写字母打印第一个字符 上() 函数,然后使用 标题() 函数,该函数自动将第一个字母表转换为大写。
# python program to print initials of a name def name(s): # split the string into a list l = s.split() new = "" # traverse in the list for i in range ( len (l) - 1 ): s = l[i] # adds the capital first character new + = (s[ 0 ].upper() + '.' ) # l[-1] gives last item of list l. We # use title to print first character in # capital. new + = l[ - 1 ].title() return new # Driver code s = "mohandas karamchand gandhi" print (name(s)) |
输出:
M.K.Gandhi
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END