Skip to main content
 首页 » 编程设计

python之替换字符串中的字符 (Python)

2025年05月04日24shanyou

<分区>

Possible Duplicate:
How to replace a string in a function with another string in Python?

我想输入任何字符串和任何类型的字符,如果字符是字母表,我想用“^”替换它并打印出来。

例如,如果我的输入是 replace('text-here'),我应该得到输出为 "^^^^-^^^^" .

我试过使用以下语句,但它只打印我输入的内容。请帮忙!

def replace(string): 
 
    for x in range(len(string)): 
        string.replace(string[x],"^") 
    print(string) 

我是 python 新手,不懂复杂的东西。请给我通俗易懂的答案。谢谢!