How can I print variable and string on same line in Python

0 votes

I am using python to work out how many children would be born in 5 years if a child was born every 7 seconds. The problem is on my last line. How do I get a variable to work when I'm printing text either side of it?

Here is my code:

currentPop = 312032486
oneYear = 365
hours = 24
minutes = 60
seconds = 60

# seconds in a single day
secondsInDay = hours * minutes * seconds

# seconds in a year
secondsInYear = secondsInDay * oneYear

fiveYears = secondsInYear * 5

#Seconds in 5 years
print fiveYears

# fiveYears in seconds, divided by 7 seconds
births = fiveYears // 7

print "If there was a birth every 7 seconds, there would be: " births "births"

Sep 17, 2018 in Python by bug_seeker
• 15,510 points
3,517 views

1 answer to this question.

0 votes

Use , to separate strings and variables while printing:

print "If there was a birth every 7 seconds, there would be: ",births,"births"

, in print statement separtes the items by a single space:

>>> print "foo","bar","spam"
foo bar spam

or better use string formatting:

print "If there was a birth every 7 seconds, there would be: {} births".format(births)

String formatting is much more powerful and allows you to do some other things as well, like : padding, fill, alignment,width, set precision etc

>>> print "{:d} {:03d} {:>20f}".format(1,2,1.1)
1 002             1.100000
  ^^^
  0's padded to 2

Demo:

>>> births = 4
>>> print "If there was a birth every 7 seconds, there would be: ",births,"births"
If there was a birth every 7 seconds, there would be:  4 births

#formatting
>>> print "If there was a birth every 7 seconds, there would be: {} births".format(births)
If there was a birth every 7 seconds, there would be: 4 births
answered Sep 17, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,343 views
0 votes
1 answer

How can I print an Error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Oct 12, 2018 in Python by aryya
• 7,460 points
1,039 views
0 votes
1 answer

comparing strings in Python using "==" or "is"

is is used for identity testing and ...READ MORE

answered Sep 19, 2018 in Python by SDeb
• 13,300 points
842 views
0 votes
1 answer

Print Delimited list

You can try the following and see ...READ MORE

answered Feb 28, 2019 in Python by SDeb
• 13,300 points
859 views
0 votes
2 answers
+1 vote
1 answer

Python: Print variable and string in same line

For a better understanding you can refer ...READ MORE

answered Oct 30, 2018 in Python by Priyaj
• 58,020 points
1,535 views
0 votes
1 answer

How can I print Lists in python

print is a function in Python 3.x, so ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 58,020 points
852 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP