提示用户输入然后输出相同输入的方法,将每个 space 替换为三个句点 (...)

Method which would prompt the user for input and then output that same input, replacing each space with three periods (...)

txt = input("Please type in your text here ")

txt = txt.expandtabs(3)

print(txt)

我在 python 文档中找到了这个内置方法,但它似乎无法完成工作。我应该改用哪种方法? 这里的目标是为用户输入的每个空 space 呈现 3 个句点 (...)。

expandtabs()用于设置制表符大小,不替换空格。但是,您可以使用 str.replace() 将任何给定的子字符串替换为另一个子字符串。

txt = input("Please type in your text here ")
txt = txt.replace(' ', '...')
print(txt)