Home > Software engineering >  Add names to a list without splitting in Python
Add names to a list without splitting in Python

Time:11-18

I'm trying to create a program where a user copy pastes a list of names seperated by spaces & python saves it as a list.

However, while using list.extend it splits every name into alphabets. How can I fix it so it saves each name as a string.

How can I solve it.

CodePudding user response:

You could use regex:

import re
input = "program where a user copy pastes a list of names seperated"
output = re.split(" ", input)

Result:

['program', 'where', 'a', 'user', 'copy', 'pastes', 'a', 'list', 'of', 'names', 'seperated']

CodePudding user response:

Python strings have the split function, that split by white space:

s = "foo bar baz"
s.split()
['foo', 'bar', 'baz']
  • Related