0

I am trying to concatenate string + variable + string picks up the variable from the file using Python

with open("file.txt") as fh:
for line line in fh:
d = "c:/" + xyz + ">"
print d

it prints c:/xyz > I wanted the output as "c:/xyz>". When i am trying in interactive mode it prints it fine.

2
  • > symbol comes in a new line
    – Amitabh
    Mar 30, 2017 at 8:38
  • There's probably a \n in xyz Mar 30, 2017 at 9:10

1 Answer 1

0

It's hard to understand the question because of the weird formatting, but in any case try using the format method:

 d = "c:/{}>".format(xyz.strip('\n'))
2
  • I tried this as well, i am getting the same output.
    – Amitabh
    Mar 30, 2017 at 9:12
  • You might need to explicitly strip the \n. I changed the code, try again. Mar 30, 2017 at 9:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.