简体   繁体   中英

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

How do I remove parentheses and single quotation marks from my output? There is no requirement in my code to show brackets and single quotation marks.

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

This is the actual output:

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

I don't want to output the middle brackets and quotation marks. I'm using Python 3.10, the IDE is pycharm.

][1]

The result of split is a list. If you want the str without surrounding space, you should use strip

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

Just to print you can try:

  • Supposing it's necessary to use .split()
person = "    xx   "
what_he_said = "    abc!"
print(*person.split(),'曾经说过',*what_he_said.split())

The split() function will separate the data with the [sep] parameter, it's empty by default, I assume you're using split to remove "[space-k]", but note that it will always return a separate argument list

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

will return:

[hola, como, estas]

If you really need to use the split() function, you can get its values with the index.

Eg

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

will return:

xx曾经说过,“abc!”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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