gpt4 book ai didi

python - 在字符串中的每个字母之间添加空格,然后末尾不留空格

转载 作者:行者123 更新时间:2023-12-01 04:27:24 24 4
gpt4 key购买 nike

我将如何创建一个程序,在该程序中可以在字符串中的每个字母之间添加空格,然后在末尾没有空格?这就是我到目前为止所拥有的,我不知道如何在末尾没有空格。

def spaceitout(string,amount):
amountint= int(amount)
pile= ""
for char in string:
pile= pile + char + " "*amount
print pile

最佳答案

最后使用.strip().strip() 从字符串中删除前导和尾随空格。您可以在docs中阅读更多相关信息。 .

def spaceitout(string,amount):
amountint= int(amount)
pile= ""
for char in string:
pile= pile + char + " "*amount
return pile.strip()

关于python - 在字符串中的每个字母之间添加空格,然后末尾不留空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32899068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com