Rudy Stricklan
Rudy Stricklan

Reputation: 135

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?

Upvotes: 1

Views: 5075

Answers (2)

nonamer92
nonamer92

Reputation: 1917

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

Upvotes: 1

Maximouse
Maximouse

Reputation: 4383

Two backslashes mean a literal backslash:

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

Upvotes: 2

Related Questions