-1

I have the following code:

with open(path, 'r') as f:
    data = f.readlines()
    print("x "+ data[1] + "y")

data[1] is the string from txt file. The result that I get:

>>> x 10
>>> y

but I would like "y" to be in the same line with "x 10". Is it possible to do in Python?

Thank you in advance!

Rostyslav
  • 35
  • 5

3 Answers3

1

It sounds like you would like to strip '\n' from data[1].

You can find it at this link. How to read a file without newlines?

tcglezen
  • 71
  • 5
1

You can use replace either

>>>test = "the\n line"

>>>print(test)
the
 line

>>>print(test.replace("\n",""))
the line

DDaly
  • 474
  • 6
0

And note that data[1] is the string from the txt file is not accurate. data[1] is the second line from the file.

    data = [line.rstrip() for line in f.readlines()]
Tim Roberts
  • 10,104
  • 1
  • 12
  • 14