Line break up when using f" with count varibale

Hi , i am new to Python prog, . i am trying to execute prog here the following :

ap.txt

AP-12
AP-13

python prog:

with open(“ap.txt”) as file:
count = 0
for line in file:
count += 1
print(“Line{}: {}”.format(count,line))
print(f"{line}" “192.168.1.200”)


output
Line1: AP-12

AP-12
192.168.1.200

Line2: AP-13
AP-13 192.168.1.200

i am trying to lines like this in one line : AP-12 192.168.1.200

but unable to do this ? any help will be appreciated

thanks

In order to preserve formatting, please select any code or traceback that you post and then click the </> button.

print normally prints a newline at the end of what it prints. If you don’t want it do that, add the keyword argument end="":

print("Line{}: {}".format(count,line), end="")
print(f"{line}" "192.168.1.200")

Thanks Matthew

still showing :

Line1: AP-12
AP-12
192.168.1.200

if you consider print (f"{line}" “192.168.1.200”, end=" ")

output :
AP-12
192.168.1.200 AP-13 192.168.1.200

not sure

i have got an option to replace the break line

    line = line.replace("\n", "")

There is built-in function enumerate which is suitable for numerating lines, something along these lines:

>>> spam = "bacon"
>>> for i, char in enumerate(spam, start=1):
...     print(f"Character {i}: {char}")
...
Character 1: b
Character 2: a
Character 3: c
Character 4: o
Character 5: n

Lines in files end with newline. These can be stripped: from end using str.rstrip() or from both ends str.strip(), combined with enumerate something like below :

>>> data = ['192\n', '193\n', '194\n']
>>> for row in data:
...     print(row)
...
192

193

194

>>> for i, row in enumerate(data, start=1):
...     print(f"Line {i}: {row.rstrip()}")
...
Line 1: 192
Line 2: 193
Line 3: 194
1 Like