if __name__ == '__main__':
string =[' \n Boeing Vancouver\n ', '\n Airbus\n ', '\n Lockheed Martin\n ', '\n Rolls-Royce\n ', '\n Northrop Grumman\n ', '\n BOMBARDIER\n ', '\n Raytheon\n ']
for item in string:
item.replace("\n"," ")
item.strip()
print(string)
输出和输入是一样的,为什么?你知道吗
您可以使用list comprehension,例如:
代码:
测试代码:
结果:
字符串在python中是不可修改的。您可以使用列表理解简单地去掉前导或尾随的空格来创建新的列表。你知道吗
您需要再次查看the documentation:
换句话说,
item.replace("\n"," ")
本身什么也不做。你知道吗相关问题 更多 >
编程相关推荐