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)
leaf_yakitori
  • 2,232
  • 1
  • 9
  • 21
  • 3
    you mean, ```print(f"cities='{cities}'\npeople='{people}'")``` ? – Ghost Ops Aug 27 '21 at 09:26
  • @GhostOps But without the extraneous quotes and the newline. – khelwood Aug 27 '21 at 09: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 – Ghost Ops Aug 27 '21 at 09:35
  • Does this answer your question? [How can I print variable and string on same line in Python?](https://stackoverflow.com/questions/17153779/how-can-i-print-variable-and-string-on-same-line-in-python) – mkrieger1 Sep 01 '21 at 08:46

2 Answers2

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
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
2
  • You can use fstring:
cities = 200
people = 500000
print(f"cities {cities},people {people}")
  • result:
cities 200,people 500000
leaf_yakitori
  • 2,232
  • 1
  • 9
  • 21