python列表姓氏,Python程序可以打印完整姓氏的名字的缩写吗?

在这里,我们使用不同的python内置函数。首先,我们使用split().split将单词分成一个列表。然后遍历直到倒数第二个单词和upper()函数用于以大写字母打印第一个字符,然后添加最后一个单词,即名称的标题,在这里我们使用title(),title函数将第一个字母转换为大写字母。

示例Input Pradip Chandra Sarkar

Output P.C Sarkar

算法fullname(str1)

/* str1 is a string */

Step 1: first we split the string into a list.

Step 2: newspace is initialized by a space(“”)

Step 3: then traverse the list till the second last word.

Step 4: then adds the capital first character using the upper function.

Step 5: then get the last item of the list.

范例程式码# python program to print initials of a name

def fullname(str1):

# split the string into a list

lst = str1.split()

newspace = ""

# traverse in the list

for i in range(len(lst)-1):

str1 = lst[i]

# adds the capital first character

newspace += (str1[0].upper()+'.')

# l[-1] gives last item of list l.

newspace += lst[-1].title()

return newspace

# Driver code

str1=input("Enter Full Name ::>")

print("Short Form of Name Is ::>",fullname(str1))

输出结果Enter Full Name ::>pradip chandra sarkar

Short Form of Name Is ::> P.C.Sarkar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值