Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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 使用string.join()运算符时出现问题_Python - Fatal编程技术网

Python 使用string.join()运算符时出现问题

Python 使用string.join()运算符时出现问题,python,Python,我在几行代码中测试了string.join()方法: a = 1 b = 1 c = 0 superpower = [] if a == 1: superpower.append("flying") if b == 1: superpower.append("soaring") if c == 1: superpower.append("high") ", ".join(superpower) print superpower 但结果总是

我在几行代码中测试了
string.join()
方法:

a = 1
b = 1
c = 0

superpower = []

if a == 1:
        superpower.append("flying")
if b == 1:
        superpower.append("soaring")
if c == 1:
        superpower.append("high")

", ".join(superpower)

print superpower
但结果总是以常规列表的形式返回,而不是字符串。我怎样才能解决这个问题?我是python新手,非常感谢您的帮助

join(superpower)返回一个字符串,它不会将输入iterable转换为字符串。您没有使用该返回值执行任何操作:

superpower_str = ', '.join(superpower)
print(superpower_str)
可能是您想要的。

“,”。join(超级能力)
返回一个字符串,它不会将输入iterable转换为字符串。您没有使用该返回值执行任何操作:

superpower_str = ', '.join(superpower)
print(superpower_str)
这可能是你想要的