![](/img/trans.png)
[英]split python string every nth character (string and nth character are in lists)
[英]Python. How to separate string with whitespace every nth character?
您將如何在每第 n 個術語后向字符串中插入一個空格。 我需要輸出仍然是一個字符串,而不是一個列表。
這就是我希望它的樣子:
message = ("xxxxxxxxxxxx")
output = ("xxxxx xxxxx xx") #E.g every 5 characters
*編輯我的問題不涉及列表,只涉及字符串
>>> message = "xxxxxxxxxxxx"
>>>' '.join([message[i:i+5] for i in range(0, len(message), 5)])
'xxxxx xxxxx xx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.