首页 > 解决方案 > 对于字符串中的某些字符,在紧接之前插入重复

问题描述

因此,我需要能够复制用户输入的字符串中的某个字符,例如:

Input: I\ love\ bac\kslashes\
Output: I\\ love\\ bac\\kslashes\\

据我所知,我复制反斜杠,转义字符无济于事......我该怎么做呢?

标签: pythonlist

解决方案


你可以用str.replace()做到这一点:

userinput = input('Input your backslashed phrase')
replaceWith = '/'
newString = userinput.replace(replaceWith, replaceWith*2)
print(newString)

推荐阅读