Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
58 views
in Technique[技术] by (71.8m points)

How to convert input lists to string in python


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

but when I want to use this code like this :


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

what is the problem here? what should I do to get the True output and convert input lists to string?

I am amatuar in coding so simple explanation would be better.

question from:https://stackoverflow.com/questions/65948995/how-to-convert-input-lists-to-string-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

input() returns a string in python. You'd need to convert the result of input() to a list and then pass it in.

If the user inputs the list as a comma separated values (e.g. "1,2,3"), then we can parse the data like this:

inputdata = input("Enter your lst: ")
valueList = [v.strip() for v in inputdata.split(",")]
     
result = " ".join(valueList)

print(result)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...