Vinod K
Vinod K

Reputation: 2133

To split on the basis of space and special character in python

v=vi nod-u

i want to split this string to obtain

l=[vi],[nod],[u]

l.split(" ") splits on the basis of space.

And i dont know the usage of the regular expression import functions properly. Could anyone explain how to do that?

Upvotes: 0

Views: 1778

Answers (1)

Adeel Zafar Soomro
Adeel Zafar Soomro

Reputation: 1522

Are you trying to split the string to get words? If so, try the following:

>>> import re
>>> pattern = re.compile(r'\W+')
>>> pattern.split('vi nod-u')
['vi', 'nod', 'u']

Upvotes: 2

Related Questions