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)
这可能是你想要的