How do i print two numbers in python?

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.

This is just a snippet of my code:

print("Total score for %s is %s  ", name, score)

But I want it to print out:

"Total score for (name) is (score)"

where name is a variable in a list and score is an integer. This is Python 3.3 if that helps at all.

How do i print two numbers in python?

asked Mar 8, 2013 at 3:51

0

There are many ways to do this. To fix your current code using %-formatting, you need to pass in a tuple:

  1. Pass it as a tuple:

    print("Total score for %s is %s" % (name, score))
    

A tuple with a single element looks like ('this',).

Here are some other common ways of doing it:

  1. Pass it as a dictionary:

    print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
    

There's also new-style string formatting, which might be a little easier to read:

  1. Use new-style string formatting:

    print("Total score for {} is {}".format(name, score))
    
  2. Use new-style string formatting with numbers (useful for reordering or printing the same one multiple times):

    print("Total score for {0} is {1}".format(name, score))
    
  3. Use new-style string formatting with explicit names:

    print("Total score for {n} is {s}".format(n=name, s=score))
    
  4. Concatenate strings:

    print("Total score for " + str(name) + " is " + str(score))
    

The clearest two, in my opinion:

  1. Just pass the values as parameters:

    print("Total score for", name, "is", score)
    

    If you don't want spaces to be inserted automatically by print in the above example, change the sep parameter:

    print("Total score for ", name, " is ", score, sep='')
    

    If you're using Python 2, won't be able to use the last two because print isn't a function in Python 2. You can, however, import this behavior from __future__:

    from __future__ import print_function
    
  2. Use the new f-string formatting in Python 3.6:

    print(f'Total score for {name} is {score}')
    

answered Mar 8, 2013 at 3:52

BlenderBlender

279k51 gold badges425 silver badges487 bronze badges

7

There are many ways to print that.

Let's have a look with another example.

a = 10
b = 20
c = a + b

#Normal string concatenation
print("sum of", a , "and" , b , "is" , c) 

#convert variable into str
print("sum of " + str(a) + " and " + str(b) + " is " + str(c)) 

# if you want to print in tuple way
print("Sum of %s and %s is %s: " %(a,b,c))  

#New style string formatting
print("sum of {} and {} is {}".format(a,b,c)) 

#in case you want to use repr()
print("sum of " + repr(a) + " and " + repr(b) + " is " + repr(c))

EDIT :

#New f-string formatting from Python 3.6:
print(f'Sum of {a} and {b} is {c}')

answered Aug 11, 2016 at 13:07

How do i print two numbers in python?

Vikas GuptaVikas Gupta

10.3k4 gold badges30 silver badges42 bronze badges

1

Use: .format():

print("Total score for {0} is {1}".format(name, score))

Or:

// Recommended, more readable code

print("Total score for {n} is {s}".format(n=name, s=score))

Or:

print("Total score for" + name + " is " + score)

Or:

print("Total score for %s is %d" % (name, score))

Or: f-string formatting from Python 3.6:

print(f'Total score for {name} is {score}')

Can use repr and automatically the '' is added:

print("Total score for" + repr(name) + " is " + repr(score))

# or for advanced: 
print(f'Total score for {name!r} is {score!r}') 

answered Jan 18, 2018 at 11:23

How do i print two numbers in python?

In Python 3.6, f-string is much cleaner.

In earlier version:

print("Total score for %s is %s. " % (name, score))

In Python 3.6:

print(f'Total score for {name} is {score}.')

will do.

It is more efficient and elegant.

How do i print two numbers in python?

answered May 23, 2017 at 10:09

AbhishekAbhishek

4735 silver badges17 bronze badges

Keeping it simple, I personally like string concatenation:

print("Total score for " + name + " is " + score)

It works with both Python 2.7 an 3.X.

NOTE: If score is an int, then, you should convert it to str:

print("Total score for " + name + " is " + str(score))

answered Apr 1, 2015 at 20:57

Paolo RovelliPaolo Rovelli

9,0202 gold badges56 silver badges37 bronze badges

Just follow this

grade = "the biggest idiot"
year = 22
print("I have been {} for {} years.".format(grade, year))

OR

grade = "the biggest idiot"
year = 22
print("I have been %s for %s years." % (grade, year))

And forget all others, else the brain won't be able to map all the formats.

Wolf

9,3847 gold badges59 silver badges102 bronze badges

answered Sep 22, 2017 at 7:11

TheExorcistTheExorcist

1,8991 gold badge18 silver badges25 bronze badges

1

Just try:

print("Total score for", name, "is", score)

How do i print two numbers in python?

answered Jul 30, 2014 at 5:00

sarorasarora

5041 gold badge6 silver badges10 bronze badges

Use f-string:

print(f'Total score for {name} is {score}')

Or

Use .format:

print("Total score for {} is {}".format(name, score))

answered Jul 7, 2018 at 6:08

How do i print two numbers in python?

M.InnatM.Innat

13.9k6 gold badges43 silver badges78 bronze badges

2

print("Total score for %s is %s  " % (name, score))

%s can be replace by %d or %f

strickt01

3,8591 gold badge14 silver badges31 bronze badges

answered Mar 3, 2016 at 16:51

If score is a number, then

print("Total score for %s is %d" % (name, score))

If score is a string, then

print("Total score for %s is %s" % (name, score))

If score is a number, then it's %d, if it's a string, then it's %s, if score is a float, then it's %f

answered Jul 11, 2016 at 19:53

SupercolbatSupercolbat

3111 gold badge8 silver badges18 bronze badges

This is what I do:

print("Total score for " + name + " is " + score)

Remember to put a space after for and before and after is.

How do i print two numbers in python?

answered Dec 21, 2015 at 9:51

The easiest way is as follows

print(f"Total score for {name} is {score}")

Just put an "f" in front.

answered Feb 14 at 16:35

How do i print two numbers in python?

This was probably a casting issue. Casting syntax happens when you try to combine two different types of variables. Since we cannot convert a string to an integer or float always, we have to convert our integers into a string. This is how you do it.: str(x). To convert to a integer, it's: int(x), and a float is float(x). Our code will be:

print('Total score for ' + str(name) + ' is ' + str(score))

Also! Run this snippet to see a table of how to convert different types of variables!

<table style="border-collapse: collapse; width: 100%;background-color:maroon; color: #00b2b2;">
<tbody>
<tr>
<td style="width: 50%;font-family: serif; padding: 3px;">Booleans</td>
<td style="width: 50%;font-family: serif; padding: 3px;"><code>bool()</code></td>
  </tr>
 <tr>
<td style="width: 50%;font-family: serif;padding: 3px">Dictionaries</td>
<td style="width: 50%;font-family: serif;padding: 3px"><code>dict()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding: 3px">Floats</td>
<td style="width: 50%;font-family: serif;padding: 3px"><code>float()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding:3px">Integers</td>
<td style="width: 50%;font-family: serif;padding:3px;"><code>int()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding: 3px">Lists</td>
<td style="width: 50%font-family: serif;padding: 3px;"><code>list()</code></td>
</tr>
</tbody>
</table>

answered Jan 1, 2021 at 21:57

How do you print two numbers side by side in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")

How do I read 2 numbers at a time in Python?

Example -.
# taking two inputs at a time..
a, b, c = input("Enter three values: ").split().
print("Enter Your First Name: ", a).
print("Enter Your Last Name: ", b).
print("Enter Your Class: ", c).
print().
# taking three inputs at a time..
x, y, z = input("Enter three values: ").split().

How do you print two integers in the same line with space in Python?

Modify print() method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.

How do I print multiple values on one line?

To print multiple expressions to the same line, you can end the print statement in Python 2 with a comma ( , ). You can set the end argument to a whitespace character string to print to the same line in Python 3.

Tải thêm tài liệu liên quan đến bài viết How do i print two numbers in python?