0

Is it possible to split() string at " "? I have tried

x=str(input("Enter string to split: ")) #Input: one "two three"
xsplit=str.split()
print(xsplit[1])

but it returns "two" but I want it to show "two three". How to do it?

5
  • Hmm.. I think so. Thanks for your help! Commented Mar 1, 2022 at 4:01
  • Curiously, are you trying to parse a TSV file ? Commented Mar 1, 2022 at 4:01
  • Not quite. I am just messing around with discord.py Commented Mar 1, 2022 at 4:04
  • Well, I tried it in my python code, it worked. Thanks a lot! Commented Mar 1, 2022 at 4:11
  • 1
    you can use module shlex to split it. shlex.split('one "two three" "four five"') gives ['one', 'two three', 'four five']. And if you want to split only on first space then text.split(' ', 1) Commented Mar 1, 2022 at 4:19

1 Answer 1

0

This will let you split on the space as well as gather everything after the split.

x=str(input("Enter string to split: ")) #Input: one "two three"
xsplit = x.split(' ')[1:]
xjoin = ' '.join(xsplit)
print(xjoin)

Once you have split into a list you can rejoin the list into a single string with .join(xsplit)

Sign up to request clarification or add additional context in comments.

2 Comments

I think the OP was trying to respect the quotes around the second and third arguments, not just grab all elements after the first
Right, but this would still bring back the quotes right, or am I still missing something haha?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.