繁体   English   中英

如何从我的 output 中删除括号和单引号?

[英]How do I remove parentheses and single quotation marks from my output?

如何从我的 output 中删除括号和单引号? 我的代码中没有要求显示方括号和单引号。

person = "    xx   "
what_he_said = "    abc!"
print(f"{person.split()}曾经说过,“{what_he_said.split()}”")

这是实际的 output:

['xx']曾经说过,“['abc!']”

我不想 output 中括号和引号。 我正在使用 Python 3.10,IDE 是 pycharm。

][1]

拆分的结果是一个列表。 如果你想要 str 没有周围的空间,你应该使用strip

person = "    xx   "
what_he_said = "    abc!"
print(f"{person.strip()}曾经说过,“{what_he_said.strip()}”")

只是打印你可以尝试:

  • 假设有必要使用.split()
person = "    xx   "
what_he_said = "    abc!"
print(*person.split(),'曾经说过',*what_he_said.split())

split() function 将使用 [sep] 参数分隔数据,默认情况下它是空的,我假设你正在使用 split 删除“[space-k]”,但请注意它总是返回一个单独的参数列表

"hola como estas ".split(sep=" ")

将返回:

[hola, como, estas]

如果你真的需要使用 split() function,你可以通过索引获取它的值。

例如

person = "    xx   "
what_he_said = "    abc!"
print(f"{person.split()[0]}曾经说过,“{what_he_said.split()[0]}”")

将返回:

xx曾经说过,“abc!”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM