我想将字符串转换为列表
fName = input("Enter your First Name: \n")
lName = input("Enter your Last Name: \n")
location = input("Enter your Location: \n")
string = fName + " " + lName + " " + location
print(string)
print("String coverted to list :",string.split())
# Output : String coverted to list : ['name', 'name', 'location']
print("String coverted to list :\n",list(string))
# Output: String coverted to list : ['n', 'a', 'm', 'e', ' ', 'n', 'a', 'm', 'e', 'l', 'o', 'c', 'a', 't', 'i', 'o', 'n' ]
预期结果:
#Output : [ namenamelocation]
基本上,我不希望列表在空格后分开,而是希望术语字符串成为列表中的单个索引
要添加,如果不在开头添加空格,则不需要使用
.replace()
,只需使用括号[]
相关问题 更多 >
编程相关推荐