Advertisement
Advertisement


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


Question

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"
2013/06/17
1
178
6/17/2013 5:58:08 PM

Accepted Answer

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 separates 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
2020/08/12
270
8/12/2020 3:14:19 AM

two more

The First one

 >>>births = str(5)
 >>>print "there are " + births + " births."
 there are 5 births.

When adding strings, they concatenate.

The Second One

Also the format (Python 2.6 and newer) method of strings is probably the standard way:

>>> births = str(5)
>>>
>>> print "there are {} births.".format(births)
there are 5 births.

This format method can be used with lists as well

>>> format_list = ['five','three']
>>> print "there are {} births and {} deaths".format(*format_list) #unpack the list
there are five births and three deaths

or dictionaries

>>> format_dictionary = {'births': 'five', 'deaths': 'three'}
>>> print "there are {births} births, and {deaths} deaths".format(**format_dictionary) #yup, unpack the dictionary
there are five births, and three deaths
2015/05/27

Python is a very versatile language. You may print variables by different methods. I have listed below 4 methods. You may use them according to your convenience.

Example:

a=1
b='ball'

Method 1:

print('I have %d %s' %(a,b))

Method 2:

print('I have',a,b)

Method 3:

print('I have {} {}'.format(a,b))

Method 4:

print('I have ' + str(a) +' ' +b)

Method 5:

  print( f'I have {a} {b}')

Output would be:

I have 1 ball
2020/04/15

If you want to work with python 3, it's very simple:

print("If there was a birth every 7 second, there would be %d births." % (births))

As of python 3.6 you can use Literal String Interpolation.

births = 5.25487
>>> print(f'If there was a birth every 7 seconds, there would be: {births:.2f} births')
If there was a birth every 7 seconds, there would be: 5.25 births
2018/07/18

You can either use the f-string or .format() methods

Using f-string

print(f'If there was a birth every 7 seconds, there would be: {births} births')

Using .format()

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