# 2B: Arithmetic Operators and Strings

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

## Concatenation

Merriam-Webster's dictionary defines concatenate to mean "linked together. The addition operator concatenates strings, so 1+1=11 if you are adding strings.

## Concatenation Subscripts and Superscripts

One way to create subscripts and superscripts in strings is to use unicode and concatenate.  The following table gives unicode subscript and superscript values that you may often needc

 character 0 1 2 3 4 5 6 7 8 9 + - superscript U2070 U00B9 U00B2 U00B3 U2074 U2075 U2076 U2077 U2078 U2079 U207A U207B subscript U2080 U2081 U2082 U2083 U2084 U2085 U2086 U2087 U2088 U2089 U208A U208B

#We will now use concatenation to create the formula for sulfuric acid.
sulfuric_acid="H"+"\u2082"+"SO"+'\u2084'
sulfate_ion = "SO"+"\u2084\u207B\u00B2"
print(sulfuric_acid, "and", sulfate_ion)
Hello world!

Note, we can make variables for the unicode

s2,s3,s4,s5,s6,s7,s8,s9="\u2082","\u2083","\u2084","\u2085","\u2086",\
"\u2087","\u2088","\u2089"
ammonium_phosphate="(NH"+s4+")"+s3+"PO"+s4
print(ammonium_phosphate)

Hello world!
##### Exercise $$\PageIndex{1}$$

You want the following code to display

The following code does not work.  comment out the print statement and uncomment the lines above and below it, and then complete the expression for x so that the code works.

#the following line assigns subscripts to the varibles using unicode
s2,s3,s4,s5,s6,s7,s8,s9="\u2082","\u2083","\u2084","\u2085","\u2086",\
"\u2087","\u2088","\u2089"
#x=
n_nonane="CH"+s3+7*"(CH"+s2+")"+"CH"+s3
#n_nonane="CH"+s3+7*x+"CH"+s3
print(n_nonane)
Hello world!

x = "(CH"+s2+")"

## Concatenation and Web Calls

This can be can be very useful if you want to add a variable to a string.  For example, the following code takes the variable "compound_name" and adds it to a web address, which gets you the molecular weight of a chemical from the Pubchem online database.

##### Note

The following code uses the requests package which has already been installed on the LibreTexts Jupyter Hub.  If you are running your own kernel you need to run the following code:

import requests

If you do not have the requests package installed, you need to install it from the Python Package Index. This can be done in either the command line or in your IDE (Thonny)

OK, lets look at concatenation in action

compound_name = "aspirin"
uri = ("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/"
+ compound_name +"/property/MolecularWeight/txt")
print(uri)
Hello world!

Run the above code and then click the link it produces.  You get the molar mass of aspirin 180.16 (g/mol).

Now, replace the word aspirin with the word caffeine, rerun the code, click the new link, and you get 194.19, the molar mass of caffeine

##### How did this work?

Concatenation can be very useful for obtaining information off the web. In this example we used concatenation with the Pubchem PUG REST API (Power User Gateway Representational State Transfer Application Program Interface) to obtain the molar mass of a chemical.  We are going to do this for the common name, but with more effort we could also do this for synonyms. An API is like a GUI (Graphical User Interface) in that the API allows interaction between two computers while the GUI allows a person to interact with a computer. When you navigate to a webpage the web address in the hyperlink is a URL (Universal Resource Locater). Just as a webpage has a URL, you can also address items within a database by a URI (Universal Resource Identifier). The W3C (World Wide Web Consortium) set forth a RDF (Resource Descriptive Framework) that involves an RDF triple (subject, predicate, object) relationship and we are taking advantage of that to obtain the molar mass by concatenating a string into a URI.  OK, it looks complicated, but it is simple.  The subject is a molecule (which we will input and assign to a variable), the predicate is a property of a molecule (its molar mass, but we could seek other properties) and the object is the value of the molar mass for the molecule, which is the result of the query

First, place the following link into a new tab of your browser:

pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/aspirin/property/MolecularWeight/txt

Note, compound words like hydrochloric acid need a dash (hydrochloric-acid).

## String Multiplication

You can also multiply strings.  Predict the outcome of the following code, and then run it.  Did it do what you expected?

spacer=20*"*"
print(spacer)
Hello world!

Predict the output of the following code

print(25%2)
print(25*"2")
print(25//2)

Hello world!

This page titled 2B: Arithmetic Operators and Strings is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.