Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation takes place on the next line in python

Python has a simple concatenation using the + operator. But I am observing something unusual.

I tried :

final_path = '/home/user/' + path + '/output'

Where path is a staring variable I want to concatenate.

print final_path

gives me:

/home/user/path
/output

Instead of /home/user/path/output

Why is going to the next line. Is the forward slash causing the issue. I tried using the escape character as well. but It does not work.

like image 740
john Avatar asked Sep 15 '25 21:09

john


2 Answers

From the looks of your code, it may be the variable path that is the problem. Check to see if path has a new line at the end. Escape characters start with a backslash \ and not a forward slash /.

like image 193
victor Avatar answered Sep 18 '25 17:09

victor


As victor said, your path variable has '\n' added at the end implicitly, so you can do such a trick to overcome the problem:

final_path = '/home/user/' + path.strip('\n') + '/output'
like image 22
Fioletibiel Avatar answered Sep 18 '25 16:09

Fioletibiel