[英]How do I store the results of each iteration of a for loop inside a variable
I wrote the following code:我写了以下代码:
full_name = "John Francis Bean Coppola"
full_name_as_list = full_name.lower().split()
for name in (full_name_as_list):
a = name[0]
print(a)
When I run print(a) It returns:当我运行 print(a) 它返回:
j
f
b
c
I wanna define a variable like joined_results = something such that if I run print(joined_results) it returns:我想定义一个像joined_results =这样的变量,如果我运行 print(joined_results) 它会返回:
jfbc
The purpose of the code I'm trying to write is to extract the initials of a name to create an e-mail address.我尝试编写的代码的目的是提取姓名的首字母以创建电子邮件地址。 For example: John Francis Ford Coppola = jffc@gmail.com
例如:约翰·弗朗西斯·福特·科波拉 = jffc@gmail.com
full_name = "John Francis Bean Coppola"
full_name_as_list = full_name.lower().split()
initials = []
for name in (full_name_as_list):
initials.append(name[0])
#edit
print(''.join(initials))
You can use join
to join together all the items in a list.您可以使用
join
将列表中的所有项目连接在一起。
print("".join([name[0] for name in full_name_as_list]))
Here, I'm joining the items together with a blank string, but for other applications, you can join them with anything, like so:在这里,我将这些项目与一个空白字符串连接在一起,但对于其他应用程序,您可以将它们与任何东西连接起来,如下所示:
print("&".join([name[0] for name in full_name_as_list]))
would print会打印
j&f&b&c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.