How to split a string into words that do not contain spaces in python?

My line:

"This is a string" 

I want to turn it into a list:

 ["This", "is", "a", "string"] 

I use the split(" ") method, but it adds spaces as list items. Please, help,

Regards

+4
source share
3 answers
 >>> v="This is a string" >>> v.split() ['This', 'is', 'a', 'string'] 

just use split() .

+11
source

It will not add spaces as elements if you just use .split() instead of .split(' ')

 >>> "This is a string".split() ['This', 'is', 'a', 'string'] 
+3
source

As the docs say, don't pass an argument.

 >>> "This is a string".split() ['This', 'is', 'a', 'string'] 
+2
source

Source: https://habr.com/ru/post/1395555/


All Articles