1

I write a little more clearly. Here, I want each line to stick together twice. normally from

a = "Hello"
b = "World"
print(a + b)

out >

"Hello world"

It can be used, but the problem I found is as follows: My intention is to paste each line into its own repetition without the extra line.

with open('domains.txt','r') as f:
    for line in f:
        test_var = line+line
        print(test_var)

out:

"string
string
string2
string2
string3
string3"

so I want this output (without new line between that):

"string string
string2 string2
string3 string3"
2
  • you want every 2 items concat and then add new line ? Commented Aug 30, 2022 at 21:56
  • Please don't lie about your outputs. Actually run the codes and show the real outputs. Commented Aug 30, 2022 at 22:11

1 Answer 1

2

print() has an optional end parameter which defaults to \n, the newline character. If you specify print(Test_var, end=''), you'll be able to print without any space between the outputs. Then, you can manually print('\n') to control where your new lines end up!

That being said, I think you're also fighting a separate issue where the file is being read in with the newline characters still appended to the end of the line. Check out How to read a file without newlines? for advice on removing newline characters from the lines you're reading.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.