Python练习题!!!!在线等

Write a Python program that asks the user to enter full name in the following format:
LastName, FirstName MiddleName
(for Example: "Hun, Attila The”).
The input will have a single space after the comma and only a single space between the first name and the middle name. Use string methods and operators in Python to convert this to a new string having the form:
FirstName MiddleInitial Period LastName (for example: "Attila T. Hun") and output it.
Be sure to test it with more than one input and not just "Hun, Attila The".
Sample Output 1:
Enter name in [LastName, FirstName MiddleName] format >Newton, Sir Isaac
The new name is : Sir I. Newton
Sample Output 2:
Enter name in [LastName, FirstName MiddleName] format >Hun, Attila The
The new name is : Attila T. Hun

能否翻译下呢?哈哈.............

#python 3.3 ä¸‹çš„ script
import re

print('enter full name(s) in the following format:\r\nLastName, FirstName MiddleName\r\nFor Example:Hun, Attila The\r\nEnter ok to submit\r\n')
line = input()
names = []
while(True):
    if(line.lower()=='ok'):
        break
    pattern = re.compile(r'\w*, \w* \w*')
    if pattern.match(line):
        names.append(line)
    else:
        print('error format for "',line,'"')
    line = input()
for name in names:
    name = name.replace(',','.')
    listName = name.split(' ')
    print(listName[1],listName[2][0]+listName[0][-1],listName[0][:-1])追问

没看懂。。。

追答

你用什么版本的python呢?

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-09-26
字符串处理。。。追问

能具体点么

追答

lName = raw_input('Please enter your last name:')
fName = raw_input('Please enter your first name:')
mName = raw_input('Please enter your middle name:')
allName = "%s, %s %s"%(lName, fName, mName);
print allName
allName2 = fName + " " + mName[0:1] + ". " + lName#transform
print allName2

追问

#Enter name in [LastName, FirstName MiddleName] format >Newton, Sir Isaac
#The new name is : Sir I. Newton

output是这样。。。

追答

上面的代码不就是Sampl1, Sample2所要求的吗。

自己加个提示信息就可以了---

lName = raw_input('Please enter your last name:')
fName = raw_input('Please enter your first name:')
mName = raw_input('Please enter your middle name:')
allName = "%s, %s %s"%(lName, fName, mName);
print "#Enter name in [LastName, FirstName MiddleName] format >" + allName
allName2 = fName + " " + mName[0:1] + ". " + lName#transform
print "#The new name is :" + allName2

追问

题目是要求 Lastname, middle name和first name 一起输入,然后直接出结果。。

追答

哦 没细看。

print('Please enter your last name')
allName = ""
while(True):
    allName = raw_input()
    pattern = re.compile(r'[A-Z][a-z]*, [A-Z][a-z]* [A-Z][a-z]*')
    if pattern.match(allName):
        break
    else:
        print('format error, Please enter your name like "Hun, Attila The"')
[lName, fName, mName] = re.split(r"[, ]+", allName)
print allName
allName2 = fName + " " + mName[0:1] + ". " + lName#transform
print "#The new name is :" + allName2

第2个回答  2014-09-27
full_name = raw_input('input full name:')
last, first, middle = full_name.split()
print '%s %s. %s' % (first, middle[0], last[:-1])

相似回答