0

I want to print text to tell what the numbers mean when l run the code. As the code is now it only show the numbers, l want it to have cities 200, people 500000

Code:

cities = 200
people = 500000
print(cities,people)
4
  • 3
    you mean, print(f"cities='{cities}'\npeople='{people}'") ? Commented Aug 27, 2021 at 9:26
  • @GhostOps But without the extraneous quotes and the newline. Commented Aug 27, 2021 at 9:32
  • @khelwood yeah, i just did it for some pretty printed output, but it's upto OP. he can modify the code as he wanted Commented Aug 27, 2021 at 9:35
  • Does this answer your question? How can I print variable and string on same line in Python? Commented Sep 1, 2021 at 8:46

2 Answers 2

3

Try this:

cities = 200
people = 500000

# (1)
print("cities %d ,people %d" % (cities, people))

# (2)
print("cities {} ,people {}".format(cities, people))

# (3)
print("cities {num1} ,people {num2}".format(num1=cities, num2=people))

# (4)
print("cities", cities, ",people", people)

# (5)
print("cities " + str(cities) + " ,people " + str(people))

# (6)
print(f"cities {cities} ,people {people}")

Output:

cities 200 ,people 500000
Sign up to request clarification or add additional context in comments.

Comments

2
  • You can use fstring:
cities = 200
people = 500000
print(f"cities {cities},people {people}")
  • result:
cities 200,people 500000

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.