How to print a variable and a string on the same line in Python?

I use python to determine how many babies will be born after 5 years if the baby is born every 7 seconds. The problem is in my last line. How to make a variable work when I type text on each side?

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" 
+126
variables python string printing
Jun 17 '13 at 17:58
source share
16 answers

Use to separate lines and variables during printing:

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

, in a print statement splits the elements into one space:

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

or better to use string formatting :

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

Formatting strings is much more powerful and allows you to do other things, such as filling, filling, aligning, width, specified accuracy, etc.

 >>> print "{:d} {:03d} {:>20f}".format(1,2,1.1) 1 002 1.100000 ^^^ 0 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 
+192
Jun 17 '13 at 17:58
source share

two more

The first

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

When you add lines, they are combined.

Second

Also, the format string method (Python 2.6 and newer) is probably the standard way:

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

This format method can also be used with lists.

 >>> 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 
+46
Jun 17 '13 at 18:06 on
source share

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

 print("If there was a birth every 7 second, there would be %d births." % (births)) 
+22
Aug 14 '16 at 10:53 on
source share

You can use the string format:

 print "There are %d births" % (births,) 

or in this simple case:

 print "There are ", births, "births" 
+12
Jun 17 '13 at 17:59
source share

Python is a very versatile language. You can print variables in many ways. I have listed 4 methods below. You can use them as you wish.

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) 

The output will be:

 I have 1 ball 
+12
Jul 05 '18 at 9:20
source share

Starting with 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 
+7
Jul 18 '18 at 16:19
source share

In the current version of Python, you should use parentheses, like so:

 print ("If there was a birth every 7 seconds", X) 
+3
Jul 12 '16 at 5:49
source share

First you have to make a variable: for example: D = 1. Then do it, but replace the line with what you want:

 D = 1 print("Here is a number!:",D) 
+3
Jan 30 '17 at 17:17
source share

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

Using f-string

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

Using .formt ()

 print("If there was a birth every 7 seconds, there would be: {births} births".format(births=births)) 
+2
Feb 12 '19 at 8:07
source share

You can use string formatting for this:

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

or you can give print few arguments, and it will automatically separate them with a space:

 print "If there was a birth every 7 seconds, there would be:", births, "births" 
+1
Jun 17 '13 at 17:59
source share

I copied and pasted your script into a .py file. I ran it as it is with Python 2.7.10 and got the same syntax error. I also tried the script in Python 3.5 and got the following output:

 File "print_strings_on_same_line.py", line 16 print fiveYears ^ SyntaxError: Missing parentheses in call to 'print' 

Then I changed the last line in which it prints the number of births as follows:

 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: " + str(births) + " births" 

The output was (Python 2.7.10):

 157680000 If there was a birth every 7 seconds, there would be: 22525714 births 

Hope this helps.

+1
Jun 17 '16 at 18:21
source share

use string formatting

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

if you are doing a toy project, use:

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

or

 print('If there was a birth every 7 seconds, there would be: %d births' %(births)) # Will replace %d with births 
+1
Jul 18 '18 at 5:40
source share

If you are using Python 3.6 or later, f-string is the best and easiest

 print(f"{your_varaible_name}") 
+1
Apr 24 '19 at 10:49
source share

A little different: using Python 3 and print several variables on one line:

 print("~~Create new DB:",argv[5],"; with user:",argv[3],"; and Password:",argv[4]," ~~") 
0
Jan 25 '19 at 12:18
source share

Python 3

Better to use format option

 user_name=input("Enter your name : ) points = 10 print ("Hello, {} your point is {} : ".format(user_name,points) 

or declare input as a string and use

 user_name=str(input("Enter your name : )) points = 10 print("Hello, "+user_name+" your point is " +str(points)) 
0
Jul 16 '19 at 7:42
source share

Just use, (comma) between them.

See this code for a better understanding:

Weight converter pounds to kg

weight_lbs = input ("Enter the weight in pounds:")

weight_kg = 0.45 * int (weight_lbs)

print ("You are", weight_kg, "kg")

0
Jul 21 '19 at 14:32
source share



All Articles