【发布时间】:2020-06-05 23:13:31
【问题描述】:
我正在学习 Python,对打印命令有疑问。
为什么在以下情况下打印命令的代码在一行中工作:
text = "The vegetables are in the fridge."
print(text.replace("vegetables", "fruits"))
但是当我这样写时,我没有得到任何结果?
numbers = [12, 34, 23, 88, 1, 65]
fruits = ["apple", "pear", "orange", "grapes", "mango"]
print(fruits.extend(numbers))
正确的方法如下:
numbers = [12, 34, 23, 88, 1, 65]
fruits = ["apple", "pear", "orange", "grapes", "mango"]
fruits.extend(numbers)
print(fruits)
我的意思是,如果逻辑如下,第一个函数起作用,然后是第二个,那么为什么在第一个函数中它才起作用?
希望我能解释一下。
先谢谢了,
莉莉丝
【问题讨论】:
-
因为
replace返回的是替换后的字符串,而extend将指定的列表元素添加到当前列表的末尾而不返回。
标签: python python-3.x printing command