简体   繁体   中英

Use Python to replace forward slashes in string to backslashes

Using Python 3.7, I have a string s defined as s = '//10.0.0.3/research' . I need some operator on s to produce '\\\\\\10.0.0.3\\research' as the output.

I understand about backslashes being escape characters, but I cannot for the life of me figure out what the proper s.replace() statement would look like to produce what I want (I need the backslashes because that's what the DOS 'net use' command needs to see when assigning UNC paths to drive letters). Ideas?

两个反斜杠表示字面反斜杠:

s.replace("/", "\\")

don't forget to assign it back to s :

s = '//10.0.0.3/research'

s = '\\' + s.replace("/", "\\")
print(s)

outputs:

\\10.0.0.3\research

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM