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 3,416 views

## 1 answer to this question.

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
^^^

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```
• 58,090 points

## How can I print literal curly-brace characters in python string and also use .format on it?

You need to double the {{ and }}: >>> x = ...READ MORE

## How i can print and count all the all element when i search like " Automation step by step" in Google using python selenium

I am trying to count all the ...READ MORE

## 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

## How can I print an Error in Python?

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

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

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

## Print Delimited list

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

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote