coma separator is printed on a new line for some reason - 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: coma separator is printed on a new line for some reason (/thread-41538.html) |
coma separator is printed on a new line for some reason - tester_V - Feb-02-2024 Gentlemen! Ladies if any?! I have never seen it before, I'm formatting a string, separating vars by a coma, and for some reason, the last coma is printed on a new line... print(f"{str(starttime)},{str(endtime)},{str(cell_nm)},{str(hand_nm)},{str(tt)},{str(prod)},")I made sure the VARs all are sctrings but still last come is printed on a new line... I even tried this: coma = "," print(f" PRODUCT = {prod}{coma}")But still, the coma printed on a new line... I'm confused how to go around it... Any help is appreciated. Thank you! RE: coma separator is printed on a new line for some reason - tester_V - Feb-02-2024 Dear God! The problem was in Var {prod}. Here how I fixed it: ss = f"{starttime},{endtime},{cell_nm},{hand_nm},{tt},{prod}" ss = ss.strip() print(f"** {ss},")Anyway, I'm glad I solved it. Thank you! Have a good night.... RE: coma separator is printed on a new line for some reason - DeaD_EyE - Feb-02-2024 I guess prod has at the end of the str a newline character.You can strip it. I often use rstrip to remove only on the right side of the str whitespace. A newline character is also seen as a whitespace.delimiter = "," product_name = "Test 123" product_name_newline = "Test 123\n" print("Test without newline") print(f"{product_name}{delimiter}") print() print("Test with newline at the end of the str") print(f"{product_name_newline}{delimiter}") print() print("Test with newline at the end of the str and the prevention") print(f"{product_name_newline.rstrip()}{delimiter}") print() RE: coma separator is printed on a new line for some reason - deanhystad - Feb-02-2024 This is a problem you should figure out yourself. Knowing that the newline must be coming from somewhere the first thing I would try is this: print(f"{prod},") print(f"{prod.strip()},"I would expect the first print to have the last comma on a new line, and the second to have the last comma on the same line. Now you know that the prod str has a trailing newline character. You should study the print command and f'string formatting. There was no need for the str() here: print(f"{str(starttime)},{str(endtime)},{str(cell_nm)},{str(hand_nm)},{str(tt)},{str(prod)},")f'string formatting converts in brackets to a str it it is not already a str. print(f"{starttime},{endtime},{cell_nm},{hand_nm},{tt},{prod},")The above will give the exact same results. But there is no need for the f'string formatting either. print(starttime, endtime, cell_nm, hand_nm, tt, prod, '', sep=',')Also gives the exact same results. RE: coma separator is printed on a new line for some reason - tester_V - Feb-02-2024 Thank you again! I got it... |