如何在制作范围函数后将空格添加到开发变量中
How to add spaces into a developing variable after making a range function
我正在编写一个程序,询问用户有多少个名字。然后,使用 for 循环,我会询问用户每个人的姓名。最后,打印他们的全名。我所知道的是,我应该保留 运行 总共的字符串,并且我可以创建一个字符串变量来包含 my_string = ""
之类的信息。这就是我遇到的一点麻烦。
我遇到的问题是,当变量在循环内发展时,我不知道如何在变量内的字符串之间放置空格。在我的代码中,名称打印出来,但它们都在一起。因此,如果我键入“John”和“Smith”,它将打印“JohnSmith”作为我拥有的 full_name
变量。我已经尝试在循环中的 full_name
变量中添加 ""
,但没有任何改变。
full_name = ""
names = int(input("How many names do you have?: "))
for i in range(names):
next_name = input("Name: ")
full_name = full_name + next_name + ""
print(full_name)
我知道这是初学者的东西,但我一周前就开始学习 Python。谢谢。
我想你只需要 join
类似这样的东西(strip
在这里清除输入)
name_words = [input("Name: ").strip() for _ in range(names)]
print(" ".join(name_words)
要在两个字符串之间放置 space,您可以显式添加 space:
str1, str2 = 'hello', 'world'
print(str1 + ' ' + str2)
虽然使用字符串格式是更好的做法:
print('{} {}'.format(str1, str2))
# or f-strings (python 3.5+)
print(f'{str1} {str2}')
最后,对于动态字符串格式化(你不知道你会得到多少参数),你可以使用str.join
:
print(' '.join((str1, str2)))
# or with something like a list comprehension
choices = [input("Name: ").strip() for _ in range(names)]
print(' '.join(choices))
我想这就是你需要的。
full_name = ""
names = int(input("How many names do you have?: "))
for i in range(names):
next_name = input("Name: ")
full_name = full_name + " " + next_name
print(full_name)
一个有趣的选择是让 print
和 unpack 进行格式化工作:
names = []
count = int(input("How many names do you have?: "))
for i in range(count):
names.append(input("Name: "))
print(*names)
这是可行的,因为 print
function uses space (' '
) as the default separator for its arguments whereas the unpack operator (*
) 将列表变量转换为位置参数列表。
我正在编写一个程序,询问用户有多少个名字。然后,使用 for 循环,我会询问用户每个人的姓名。最后,打印他们的全名。我所知道的是,我应该保留 运行 总共的字符串,并且我可以创建一个字符串变量来包含 my_string = ""
之类的信息。这就是我遇到的一点麻烦。
我遇到的问题是,当变量在循环内发展时,我不知道如何在变量内的字符串之间放置空格。在我的代码中,名称打印出来,但它们都在一起。因此,如果我键入“John”和“Smith”,它将打印“JohnSmith”作为我拥有的 full_name
变量。我已经尝试在循环中的 full_name
变量中添加 ""
,但没有任何改变。
full_name = ""
names = int(input("How many names do you have?: "))
for i in range(names):
next_name = input("Name: ")
full_name = full_name + next_name + ""
print(full_name)
我知道这是初学者的东西,但我一周前就开始学习 Python。谢谢。
我想你只需要 join
类似这样的东西(strip
在这里清除输入)
name_words = [input("Name: ").strip() for _ in range(names)]
print(" ".join(name_words)
要在两个字符串之间放置 space,您可以显式添加 space:
str1, str2 = 'hello', 'world'
print(str1 + ' ' + str2)
虽然使用字符串格式是更好的做法:
print('{} {}'.format(str1, str2))
# or f-strings (python 3.5+)
print(f'{str1} {str2}')
最后,对于动态字符串格式化(你不知道你会得到多少参数),你可以使用str.join
:
print(' '.join((str1, str2)))
# or with something like a list comprehension
choices = [input("Name: ").strip() for _ in range(names)]
print(' '.join(choices))
我想这就是你需要的。
full_name = ""
names = int(input("How many names do you have?: "))
for i in range(names):
next_name = input("Name: ")
full_name = full_name + " " + next_name
print(full_name)
一个有趣的选择是让 print
和 unpack 进行格式化工作:
names = []
count = int(input("How many names do you have?: "))
for i in range(count):
names.append(input("Name: "))
print(*names)
这是可行的,因为 print
function uses space (' '
) as the default separator for its arguments whereas the unpack operator (*
) 将列表变量转换为位置参数列表。