Python Forum
Concat Strings - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Concat Strings (/thread-36948.html)



Concat Strings - paulo79 - Apr-15-2022

Hi all,

Trying to concat this variable with strings but it is addind a line in output:
>>> del v_first_cell_name
>>> v_first_cell_name=subprocess.check_output("cat /tmp/p_f | sort -u | head -1", shell=True);
>>> v_first_cell_name=("physicaldisks_"+v_first_cell_name+".xml")
>>> print(v_first_cell_name)
physicaldisks_CELADM04
.xml
>>>
How to not ADD the ".xml" as new line, I mean, I want to add in same like to stay like physicaldisks_CELADM04.xml


RE: Concat Strings - deanhystad - Apr-15-2022

Guess this is not related to your problem. But I have seen this using subprocess on Windows:

You are not concatenating strings. You are concatenating bytes and strings. You need to convert the bytes object returned by the subprocess call to a str.
Output:
>>> v_first_cell_name = subprocess.check_output("TYPE data.csv", shell=True) >>> print(v_first_cell_name) b'this_is_a_test' #<- See! This is a bytes object, not str >>> v_first_cell_name = v_first_cell_name.decode() # <- Convert bytes to str >>> print(v_first_cell_name) this_is_a_test #<- Now it is a str. no b'' >>> print("some prefix" + v_first_cell_name + "some suffix") some prefixthis_is_a_testsome suffix



RE: Concat Strings - Gribouillis - Apr-15-2022

Use .rstrip() to remove possible white space, including newline at the end
>>> 'CELADM04\n'.rstrip()
'CELADM04'
>>> 



RE: Concat Strings - deanhystad - Apr-15-2022

Is getoutput() returning bytes instead of a str a windows thing? Or is it related to the command you execute?


RE: Concat Strings - Gribouillis - Apr-15-2022

It seems to return bytes in linux too, but I don't know where it is documented.


RE: Concat Strings - snippsat - Apr-15-2022

@paulo79 You most tell that for some reason still use Python 2.7💀
All answer you get here is for Python 3.
So deanhystad dos not info and answers for Python 3.
Python 2.7 did not return a bytes object as Python 3 dos.