2

i have a path, i need to include variables in between that path as shown below

import os

platform = "ppc"
variant = "red"
A2L = "\frdcc_hyb_sw\hn1\output\hn1_" +platform+ "_sil\r" + variant
print A2L

i get a weird output as shown below

C:\app\Tools\exam\Python25>python new.py
redcc_hyb_sw\hn1\output\hn1_ppc_sil

4
  • Can you please turn this into a question? :-) Commented Oct 29, 2015 at 13:42
  • hah sorry i need to get a path as \frdcc_hyb_sw\hn1\output\hn1_ppc_sil\red. According to me i have done it correct, i have no idea why is it acting starge. can you please help with my weird output Commented Oct 29, 2015 at 13:44
  • I don't know python, but could that \r be interpreted as "carriage return"? Maybe you need to escape backslashes. Commented Oct 29, 2015 at 13:45
  • Also, when you have additional info, please edit it into your question Commented Oct 29, 2015 at 13:46

1 Answer 1

1

That's because of escape characters use it like so (Look also for the format example which is best practice), If you want the charecter to be escaped like \n for new line then just leave it as \n instead of \\n

In Short:

\n in string == new line

\\n in string == the notes "\n"

Code:

platform = "ppc"
variant = "red"
A2L = "\\frdcc_hyb_sw\\hn1\\output\\hn1_" +platform+ "_sil\\r" + variant
print A2L

A2L = "\\frdcc_hyb_sw\\hn1\\output\\hn1_{}_sil\\r{}".format(platform, variant)
print A2L

Output:

\frdcc_hyb_sw\hn1\output\hn1_ppc_sil\rred
\frdcc_hyb_sw\hn1\output\hn1_ppc_sil\rred
Sign up to request clarification or add additional context in comments.

2 Comments

how do i make it if it's somthng like "c:\abc\red\gfd\afd\"
For path use os.path.join('my', 'path', 'to', 'join')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.