Adding spaces to items in list (Python)

前端 未结 7 1866
北海茫月
北海茫月 2020-12-19 10:12

I\'m a Python noob and I need some help for a simple problem.

What I need to do is create a list with 3 items and add spaces before and after every item.

For

相关标签:
7条回答
  • 2020-12-19 10:47
    [ ' {} '.format(x) for x in lst ]
    

    EDIT for python 3.6+:

    you can use f-strings instead, see docs: https://www.python.org/dev/peps/pep-0498/

    the example above would look like:

    [ f' {x} ' for x in lst ]
    
    0 讨论(0)
提交回复
热议问题