0

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
6
  • 2
    os.path.normpath()? Commented Jan 4, 2016 at 16:28
  • str = C:\Users\PB\Desktop\Arsiv is not valid Python syntax. Are you sure you tagged the right language? Commented Jan 4, 2016 at 16:29
  • s = "C:\Users\PB\Desktop\Arsiv"; print(re.escape(s)) Commented Jan 4, 2016 at 16:37
  • str.replace('\', '\\') Commented Jan 4, 2016 at 17:07
  • @vaultah I need to save an image to a folder . I get the path from database and it gives me the first string . So I need to add '\\' character for adding image to the folder Commented Jan 4, 2016 at 17:37

2 Answers 2

1

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

astr.replace("\\", "\\\\")
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

Raw strings are used to not enter escape-sequence hell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.