Home > front end >  How do I remove parentheses and single quotation marks from my output?
How do I remove parentheses and single quotation marks from my output?

Time:02-11

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.

enter image description here

CodePudding user response:

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()}”")

CodePudding user response:

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())

CodePudding user response:

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.

E.g

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

will return:

xx曾经说过,“abc!”
  • Related