2013-03-22 38 views
0

我必须创建一个代码,将域名分割为用户名。在Python中将一行分隔为两部分

ex。

输入[email protected]

输出:您的用户名是ABC。 您的域名是xyz.com。

结果是假设在不同的线路通过,但我似乎无法得到那个......

def username2(email): 
    z=(email.split('@')) 
    x='Your username is'+ ' ' + z[0] 
    y='Your domain is' + ' ' + z[1] 
    return x+'. '+y+'.' 

对不起..我真的很小白。

回答

4

你需要插入一个换行符到您的结果:

return x + '. \n' + y + '.' 

你也可以使用字符串格式化:

username, domain = email.split('@') 

return 'Your username is {}.\nYour domain is {}.'.format(username, domain) 
+0

+1提 “” .format() – David 2013-03-22 01:09:09

0

Python3

def username2(email): 
    username, domain = email.split('@') 
    print('Your username is {}'.format(username)) 
    print('Your domain is {}'.format(domain)) 

Python2

def username2(email): 
    username, domain = email.split('@') 
    print 'Your username is %s' % username 
    print 'Your domain is %s' % domain