Ashok
Ashok

Reputation: 115

Adding a variable to a path

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

Upvotes: 2

Views: 77

Answers (1)

Kobi K
Kobi K

Reputation: 7931

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

Upvotes: 1

Related Questions