Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将输入列表转换为字符串_Python_String_List_Converters - Fatal编程技术网

如何在python中将输入列表转换为字符串

如何在python中将输入列表转换为字符串,python,string,list,converters,Python,String,List,Converters,但当我想这样使用代码时: L = [1,2,3] q = " ".join(str(x) for x in L) print(q) # as you see input is = L = [1,2,3] # when you run the code output = 1 2 3 这里有什么问题?我应该怎么做才能获得真正的输出并将输入列表转换为字符串 我对编码很在行,所以简单的解释会更好。input()返回python中的字符串。您需要将input

但当我想这样使用代码时:


L = [1,2,3]       

q = " ".join(str(x) for x in L)

print(q)

# as you see input is = L = [1,2,3]

# when you run the code output = 1 2 3

这里有什么问题?我应该怎么做才能获得真正的输出并将输入列表转换为字符串

我对编码很在行,所以简单的解释会更好。

input()
返回python中的字符串。您需要将
input()
的结果转换为
列表
,然后传入

如果用户以逗号分隔的值(例如“1,2,3”)输入列表,那么我们可以如下解析数据:

inputdata=input(“输入您的lst:”)
valueList=[v.strip()表示inputdata.split中的v(“,”)]
结果=”.join(值列表)
打印(结果)

L=input(“输入您的lst:”)
中,
L
已经是一个string@python_user当然可以,只输入不带括号的数字。逗号和空格可以让您自己(并阅读官方文档!)获得所需的输出,
input
语句返回什么?提示:@IronFist我只是想知道为什么会发生这种事!好。谢谢你,兄弟。现在有一件事我得到的结果是=[1 2 3],我可以删除“[]”这个东西吗?您可以使用
strip()
删除任何尾随空白,然后切片:
inputdata=input(“输入您的lst:”).strip()[1:-1]

L = input("Enter your lst: ")
     
q = " ".join(str(x) for x in L)

print(q)

# I enter my input again = [1,2,3]

# and the output is = [ 1 , 2 , 3 ]

# but I was looking for this : 1 2 3