Python中如何移除输出列表元素外侧的单引号?
原因说明
你观察到的单引号并不是列表元素本身的内容,是Python在打印字符串类型的列表元素时,默认添加的标识,用于区分字符串类型和其他类型的元素,实际存储在列表里的元素是不带单引号的。你可以通过打印单个元素验证:
msg1 = " Facebook already uses AI to Filter Fake stories from the feeds of users" words = msg1.split() print(words[0]) # 输出结果为:Facebook,无单引号
格式化输出方案
如果需要得到你要求的展示格式,手动拼接输出字符串即可,参考代码如下:
msg1 = " Facebook already uses AI to Filter Fake stories from the feeds of users" words = msg1.split() # 按期望格式拼接字符串 format_output = "[" + words[0] + " , " + ", ".join(words[1:]) + "]" print(format_output)
运行后输出完全匹配你的预期:[Facebook , already, uses, AI, to, Filter, Fake, stories, from, the, feeds, of, users]
内容的提问来源于stack exchange,提问作者AKSHAY ARJUN
相关产品推荐
相关产品推荐

