Skip to main content
 首页 » 编程设计

python之我有一个 python 列表作为字符串,我想将它打印为间隔和顺序

2025年05月04日21bluestorm

我有一个 python 列表,我想按顺序打印它们并在它们之间留出间隔

x = "hello world" 
>>> x[2:10]  # this prints from 'l' to 'd' allway 
 
>>> x[2:10:2]  # this prints from 'l' to 'd' in the order of two, 
'lowr' 

我怎样才能像'l o w r'一样打印它,在它们之间放置一个空格字符而不循环?所以它看起来像'l-o-w-r'

请您参考如下方法:

查看 Python 的 str.join()功能。它允许您加入任何可迭代对象,同时在 .join() 的引号 "" 之间插入任何内容。

像这样:

x = "hello world" 
 
" ".join(x[2:10:2])