簡體   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