Python Arithmetic

Python Arithmetic For Strings

In the previous section, we learned how Python can do arithmetic using numbers and variables.  In this section, let us see how we can do arithmetic on string literals and string variables.

Just as you can do integer addition using the plus (+) symbol and number multiplication using the multiply (*) symbol, you can use the arithmetic operators (add and multiply) to do string addition (concatenation) and string multiplication (repetition of strings).

String Addition (concatenation) of String Literals

Continuing with the dinosaur’s facts, we will do string literal addition of four string literals using the plus (+) operator.

Two or more string literals adjacent to each other are joined automatically by Python. In the program below, four string literals

  1. ‘Brontosaurs means Thunder Lizard. ‘
  2. ‘It was a gigantic quadruped dinosaur. ‘
  3. ‘It weighed 15 metric tons. ‘
  4. ‘It was 22 meters (72 feet} long.’

are written next to each other after the Python prompt in the first statement. Python joined them together to make a full paragraph.

PROGRAM EXAMPLE: STRING LITERALS CONCATENATION BY WRITING STRINGS NEXT TO EACH OTHER
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> # String Literal Addition (Concatenation) by writing the literals next to each other.
>>> 'Brontosaurs means Thunder Lizard. ' 'It was a gigantic quadruped dinosaur. ' 'It weighed 15 metric tons. ' 'It was 22 meters (72 feet) long.'
'Brontosaurs means Thunder Lizard. It was a gigantic quadruped dinosaur. It weighed 15 metric tons. It was 22 meters (72 feet} long.'
>>> 

String Literals can also be joined (concatenated) together using plus (+) arithmetic operator as in the program below.

PROGRAM EXAMPLE: STRING LITERAL CONCATENATION USING ADDITION (+) SYMBOL
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> # String literal concatenation using + symbol.
>>> 'Brontosaurs means Thunder Lizard. ' +  'It was a gigantic quadruped dinosaur. ' + 	'It weighed 15 metric tons. ' +	'It was 22 meters (72 feet} long.'
'Brontosaurs means Thunder Lizard. It was a gigantic quadruped dinosaur. It weighed 15 metric tons. It was 22 meters (72 feet} long.'
>>>>>>

Python Arithmetic for String Variables – Addition (Concatenation)

Just like string literal addition, String variables can also be added using the plus (+) operator as in example below.

First, we will define three string variables:

  • First_name
  • Last _name
  • Full_name.

Then, we will have the Python program concatenate First_name and Last_name using the + operators to make Full_name.

PROGRAM EXAMPLE: STRING VARIABLE CONCATENATION
>>> # String Variable concatenation using + symbol.
>>> First_name = 'Robert '
>>> Last_name = 'Jones'
>>> Full_name = First_name + Last_name
>>> print(Full_name)
Robert Jones
>>>

Python Arithmetic: String Multiplication

Now let us see how we can do string multiplication.

Let us suppose that in the last soccer tournament, your school was one of the two finalists.  In the finals match, you scored five goals for your school resulting in your school’s victory.  In the excitement of your victory, you shouted ‘We won’ five times.  Suppose you want to write a Python program to say ‘We won’ five times, print it out, and send it to all your team members.

The multiply operator (*) can be used to repeat string characters any number of times as in the following program.

PROGRAM EXAMPLE: STRING MULTIPLICATION (REPETITION)
>>> Example 1
>>> # String literal multiplication Example Program
>>> 5*'We won. '
'We won. We won. We won. We won. We won. '
>>> Example 2
>>> # Use of parenthesis to show arithmetic operator precedence. 
>>> # Parenthesis is evaluated first before multiply.
>>> 2*('I like ' + 'playing soccer. ')  
'I like playing soccer. I like playing soccer. '
Example 3
>>> # You can also use string multiplication without using plus operator.
>>> 2*'I like ' 'playing soccer. '
'I like playing soccer. I like playing soccer. '
>>>

Note that in Example 2, a parenthesis is evaluated first before the addition (+) is done.

In Example 3, the strings literal are joined first before the joined string is multiplied by 2.

Saving the Program

We have written quite a few programs in the Python interactive mode.  It is a good idea to save these programs so you can run them again if you desire.  The following is the method to save your programs.

  1. Open the Python Shell IDLE window.
  2. Click on File and then New File
  3. A blank window with ‘Untitled’ name will open on your desktop
  4. Enter your program in the ‘Untitled’ window
  5. Then, save the program file you have entered in the above window by clicking on File and then Save As. Enter the file name that you desire when prompted.
  6. Lastly, click on Run, then select Run Module
  7. The saved program will run in the window.

Below is the screen shot of the above procedure, the program, and the program’s output.

Saved Program Output:

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
======================== RESTART: C:/Users/save2.py =======================
x multiplied by y =  15
>>> 

Enrichment : US and Metric Units

Units of Measurement of Length and Mass Used in United States
LENGTH                      
1 Mile1,760 yds.5,280 ft.
1 Yard (yd.)3 ft.36 in.
1 Feet (ft.)12 in. 
Inches(in.)  
   
MASS  
 One Ton2,000 pounds 
One Pound (lb.)16 ounce (oz.) 
Metric Units of Measurement of Length and Mass Used in Most of the World
LENGTH  
Kilometer (km)1000 meters 
Meter (m)10 decimeters100 centimeters
Decimeter10 centimeters 
Centimeter (cm.)  
   
MASS  
Metric Ton (t)1000 kilogram 
Kilogram (kg.)1,000 gram 
Gram (gm.)  
US Units / Metric Units Conversion for Length and Mass
Metric SystemUS SystemMetric SystemFPS System
LENGTH   
One mile1.60934 kilometerOne kilometer0.621371 miles
1 yard (yd)0.9144 meter1 meter (m)1.0936 yards
1 foot (ft)0.3048 meter  
    
MASS   
1 pound (lb)   0.4536 kilogram1 kilogram (kg)2.2046 pounds
1 ounce (oz.)30.24 gram (gm.)1 metric Ton (tonne) (t)2204.6 pounds

Copyright © 2019 – 2024 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights