Code Examples: Multiply In Python

Have you just started using Python and want to learn how to Multiply in Python? In this article, I will discuss various methods that you can use to multiply numbers, decimals, floats, numbers in string format.

Multiplying in Python is really simple and it is similar to any other programming language. The basic is to use the asterisk sign between two numbers to get the multiplication of any type of numbers you have. Let us see in the below example codes.

How To Write Multiply In Python

Writing Multiply in Python is similar to any other programming language if you have ever used let us see in the below syntax for multiplying two numbers.

#Syntax
multipliedNumber = number1 * number2

How To Multiply Two Integer Number in Python

If you have two numbers stored in Python then you can create a new variable to store the multiplication of those two numbers and use the asterisk sign between them to get the multiplied value.

Let us see in the below example code how to multiply two integer numbers in Python.

#Initializing the Number
number1 = 121234
number2 = 15124

#Initializing the Multi Variable
getMulti = number1 * number2

#Priting the Multiplication of Two
#Integer Numbers
print("The multiplcation is: {}".format(getMulti))

Output:

The multiplcation is: 1833543016

As you can see in the above code, I took two large integer numbers and was able to get the multiplication value stored in the new variable using the asterisk sign between two numbers.

How To Multiply Two Decimal or Float Values in Python

Now if you have two different decimal or Float values and you want to get the multiplication values of those decimal numbers then you have to follow the same method discussed above.

Let us see the example code for multiplying two decimal numbers in Python.

#Initializing the Number
number1 = 4.324
number2 = 6.1241

#Initializing the Multi Variable
getMulti = number1 * number2


#Priting the Multiplication of Two
#Decimal Numbers
print("The multiplcation is: {}".format(getMulti))

How To Multiply String in Python

Now if you have a String and want to multiply that String to multiple values just use the Multiply sign. Suppose you have ‘1’ present in string and you want to get a string of ‘1’ to be multiplied in 5 to give the output as ‘11111’. You can do this using Python.

Let us see in the below example code how to multiply String in Python.

Output:

The multiplcation of String is: 22222222

As you can see in the above code, I was able to multiply 2 up to eight times using the multiplication sign. And hence if you have any such usability then you should use the above method.

How To Multiply Inputs in Python

If want to write a program that should take inputs as a number from the user and then provide the multiplication value. You can do this using the input method present in Python.

#Initializing the Number
number1 = input("Enter the first Number: ")
number2 = input("Enter the second Number: ")

#Initializing the Multi Variable
getMulti = float(number1) * float(number2)


#Priting the Multiplication of String 
#And Integer
print("The multiplcation of String is: {}".format(getMulti))

Output:

Enter the first Number: 10
Enter the second Number: 20
The multiplcation of String is: 200.0

As you can see in the above code I have taken the first and second numbers as user input and was able to do the multiplication using the same methods discussed above. This method can be used to multiply float and integer as well.

Code Examples: Multiply In Python

Creating Multiply Function Two multiply two numbers in Python

You can create a function as well that will take two numbers as an input and then it will return the multiplication value of the numbers.

#Creating the Function to Multiply
def multiply(number1, number2):
    return float(number1)*float(number2)

#Initializing the Number
number1 = input("Enter the first Number: ")
number2 = input("Enter the first Number: ")


#Priting the Multiplication of String 
#And Integer
print("The multiplcation of String is: {}".format(multiply(number1, number2)))

Output:

The multiplcation of String is: 168.0

Wrap Up

I hope you were able to learn the different ways how you can multiply in Python. I have listed around six different methods to get the numbers multiplied.

Let me know if you have any better method to multiply two numbers in Python in the comment section I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Join Words In a List Using Python Code Example
  2. Python Code Example: How To Find Element in a List in 6 Way
  3. How To Iterate Over Rows in a DataFrame in Pandas Python
  4. Python Get Filename From A Path Without Extension 4 Ways
  5. How To Concatenate String and Integer In Python
  6. Code Example: Convert a String to DateTime in Python

Leave a Comment