简体   繁体   中英

converting entire sentence into string into list without split

I am trying to convert a sentence into list format without space separator I tried the following way

a = 'this is me'

when I am using split to get into list format

a.split(' ')
# ['this', 'is', 'me']

list(a)
# ['t','h','i','s','m','e']

is there any way to give input as

a = 'this is me'

and get the output as

a = ['this is me']

Use this:-

>>> a = 'this is me'
>>> [a]
['this is me']

Using list made the function iterate on the string, which you didn't want. Use those big braces instead, as a list constructor.

IN: a= 'this is me'
IN: a = [a]
IN: print(a)
OUT: ['this is me']

a = 'this is me'

" ".join(a.split(" ")) will return 'this is me' and if a has leading/trailing spaces a = ' this is me ' you can use " ".join(a.strip().split(" "))

to get your solution do

a = [a.strip()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM