1

Here is what I tried

f = open('names.txt')
for people in f:
    print('{0} and {1}'.format(people, people))

Output:

john
and john

vlad 
and vlad

But my expected output is

john and john
vlad and vlad

What is wrong here and how to fix it?

3
  • 1
    what is f? I think people might have a newline char at theme end Commented May 28, 2019 at 19:51
  • 2
    Presumably each item in f ends with a newline - maybe it's a file? Commented May 28, 2019 at 19:52
  • @jonrsharpe yes. Commented May 28, 2019 at 19:54

2 Answers 2

3

How about this:

people = people.rstrip()
print('{0} and {1}'.format(people, people))
Sign up to request clarification or add additional context in comments.

Comments

2

You can try removing the newline characters:

f = open('names.txt')
for people in f:
    people = people.replace("\n", "")
    print('{0} and {1}'.format(people, people))

This way there should only be a new line on the next person in the file f.

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.