[英]how can I split a list and print it in separate lines?
Hi this is my code for Telegram bot,I have a list that fills by names. 嗨,这是我的Telegram机器人代码,我有一个按名称填充的列表。 the bot add usernames to list and reply it as a list I want to reply every name in seperate lines 机器人将用户名添加到列表中并作为列表进行回复我想在单独的行中回复每个名称
list = []
txt = str(list)
def msg_filter(bot , update):
wordsp = ["man"," miam"]
wordsn = ['nemiam','nistam','na']
if len(list) <=15:
if any (i in update.message.text for i in wordsp):
if "{}".format(update.message.from_user.first_name) not in list:
list.append("{}".format(update.message.from_user.first_name))
bot.send_message(chat_id = update.message.chat_id , text = "{}".format(list))
I have this in output : ['username1','username2','username3']
我在输出中有这个: ['username1','username2','username3']
but expect this: 但是期望这样:
[username
usernam2
username3]
you could use: 您可以使用:
print('\n'.join(list))
# please do not name your list "list", list is a build-in function !!!
文本= “[{}]”。格式( “\\ n” 个。加入(LST))
If you want to output the list new-line separated you can use #join and use the newline-character ( \\n
) as separator for your list entries. 如果要输出以换行符分隔的列表,则可以使用#join并将换行符( \\n
)用作列表条目的分隔符。
For example: 例如:
bot.send_message(chat_id = update.message.chat_id , text = "\n".join(list))
which will output: 将输出:
username1
username2
username3
If you explicitly want the [
& ]
outputed as well, you can use format to add them: 如果您还明确希望输出[
& ]
,则可以使用format添加它们:
text = "[\n{}\n]".format("\n".join(list))
bot.send_message(chat_id = update.message.chat_id , text = text)
If you need exact this result without any quotes: 如果您需要确切的结果而不带引号:
[username1
usernam2
username3]
Try this simple solution: 试试这个简单的解决方案:
print(f'[{list_[0]}')
for l in list_[1:-2]:
print(l)
print(f'{list_[-1]}]')
where list_ = ['username1','username2','username3'] 其中list_ = ['username1','username2','username3']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.