Cahit Yıldırım
Cahit Yıldırım

Reputation: 529

Adding Character in String

If I have a string like this:

str = C:\Users\PB\Desktop\Arsiv

How can I append character '\' in this string like this:

str = C:\\Users\\PB\\Desktop\\Arsiv

Upvotes: 0

Views: 50

Answers (2)

Soson
Soson

Reputation: 120

You might try raw strings by using an r or R prefix.

str = r'C:\\Users\\PB\\Desktop\\Arsiv'

Upvotes: 1

mattsap
mattsap

Reputation: 3806

You'll need to escape the \ . So, for every instance of a backslash character, replace it with 2 backslash characters.

astr.replace("\\", "\\\\")

Upvotes: 1

Related Questions