试图消除最初的 ", " - 字符串格式问题
Trying to eliminate the initial ", " - string-formatting question
我是 python 的新手(实际上是一般编码),刚接触几天。
我正在试用这段代码:
name = input("What is your name:")
print("Hello", name + "!")
list_of_fruits = list(input("Enter multiple fruits you don't like :").split(", "))
print("I don't like the following fruits:", *list_of_fruits, sep=", ")
它returns这个:
What is your name:Reg
Hello Reg!
Enter multiple fruits you don't like :apple, banana, orange
I don't like the following fruits:, apple, banana, orange
我试图消除“我不喜欢以下水果:”之后的“,”,但我似乎找不到正确的方法。似乎将 sep() 函数添加到第一个字符串“我不喜欢以下水果:”。我似乎无法找到解决方法。仅供参考,我想在一行中显示带有列表的字符串。谢谢!
这是我想要达到的更理想的结果
I don't like the following fruits: apple, banana, orange
以逗号连接字符串列表的正常方法是使用 join
方法。
print("I don't like the following fruits:", ', '.join(list_of_fruits))
我是 python 的新手(实际上是一般编码),刚接触几天。
我正在试用这段代码:
name = input("What is your name:")
print("Hello", name + "!")
list_of_fruits = list(input("Enter multiple fruits you don't like :").split(", "))
print("I don't like the following fruits:", *list_of_fruits, sep=", ")
它returns这个:
What is your name:Reg
Hello Reg!
Enter multiple fruits you don't like :apple, banana, orange
I don't like the following fruits:, apple, banana, orange
我试图消除“我不喜欢以下水果:”之后的“,”,但我似乎找不到正确的方法。似乎将 sep() 函数添加到第一个字符串“我不喜欢以下水果:”。我似乎无法找到解决方法。仅供参考,我想在一行中显示带有列表的字符串。谢谢!
这是我想要达到的更理想的结果
I don't like the following fruits: apple, banana, orange
以逗号连接字符串列表的正常方法是使用 join
方法。
print("I don't like the following fruits:", ', '.join(list_of_fruits))