---
# System prepended metadata

title: 'Week 5: Arithmetic Operations,Indexing,Slicing,String formatting And String Method In Python'

---

## **Week 5: Arithmetic Operations,Indexing,Slicing,String formatting And String Method In Python**
By *Akinlade Temitope Victory*

The past week started off with a recap of what we did the week before which was basically how to navigate vim and being introduced to python and some of it's datatypes.

We moved forward to performing basic Arithmetic operations,Indexing and Slicing in strings(text),String formatting and String Method in Python

**ARITHMETIC OPERATIONS**
These are just simply ways python takes arithmetic operations as input and gives out output in numeric datatypes like integers and floats. 
Example of these are:
```python=
  firstNumber = 4
  secondNumber = 7
  total = firstNumber + secondNumber
  OUTPUT: 11 # Integer Value
  
  # Re-initialize the variable total
  total = float(firstNumber) + float(secondNumber)
  OUTPUT: 11.0 # Float Value
```
**INDEXING**
It was explained that indexing is basically how each characters of a string(text) is located in memory and how to have access to each character of that same string(text) and example of this is:
```python=
  message = "LOVE"
  print(message[0])
  OUTPUT: L
  print(message[1])
  OUTPUT: O
  print(message[2])
  OUTPUT: V
  print(message[3])
  OUTPUT: E
```
In the above, the square braces after the variable in our print statement takes in the address of each character in the string(text) "LOVE" and prints it out to the screen.

Another way of indexing is called negative indexing which just takes in the negative number and in the case of the example, -4 is the first character(L) while -1 is the last character(E). 
Example of negative indexing:
```python=
  message = "LOVE"
  print(message[-4])
  OUTPUT: L
  print(message[-3])
  OUTPUT: O
  print(message[-2])
  OUTPUT: V
  print(message[-1])
  OUTPUT: E
```

**SLICING**
We were shown another way of accessing characters in a string and it's called slicing which is a concept that takes a starting point of the character or characters to be taken from the string(text) to the end point which does not usually take the character at the end point but the character before it and an example is:
```python=
   message = "I LOVE MUSIC"
   print(message[0:6])
   output = I LOVE
   print(message[7:])
   OUTPUT: MUSIC
```
In the above, the was no need adding an end point in printing out "MUSIC" as there is no other character or characters after and if there was, we would have to. Like indexing, slicing also takes negative numbers to print out characters of a string(text) e.g:
```python=
   message = "I LOVE MUSIC"
   print(-1:-6)
   OUTPUT: MUSIC
```

**STRING FORMATTING**
It was explained that this is a way to insert values like number and words into a string(text) in an organized way. 
Example:
```python=
   userName = Temitope
   userAge = 22
   print(f"Hello my name is {userName} and i am {userAge} years old")
   OUTPUT: Hello my name is Temitope and i am 22 years old.
```
We went further to format specification in python string formatting, and it controls how numbers are displayed especially floating(decimal) point numbers.
Example:
```python=
   pi = 3.14158
   print(f"Pi is approximately {pi:.2f}")
   OUTPUT: Pi is approximately 3.14
```

In the above, :2f is called a format specifier and:
: starts the format specification
.2 means 2 decimal places
f is the type whch is a float

**STRING METHOD**
Finally, we were introduced to string methods which is basically built in tool in python that allows you manipulate string(text) and the few ones we have worked with so far are:
> .capitalize() method which capitalizes the first character of a string(text)
> 
> .upper() method which capitalizes the whole string(text)
> 
> .split() method which breaks a string(text) into parts and puts those parts into a list(a datatype in python that is a collection of items like a container that holds things such as numbers, words or even other list)

An example of how the capitalize method is used is:
```python=
   user_name = temitope
   my_user_name = user_name.capitalize()
   print(my_user_name)
   OUTPUT: Temitope
```
An example of how the upper method is used is:
```python=
   user_name = temitope
   my_user_name = user_name.upper()
   print(my_user_name)
   OUTPUT: TEMITOPE
```
An example of how the split method is used is:
```python=
   bio = "I#AM#AKINLADE#TEMITOPE"
   user_bio = bio.split("#")
   print(user_bio)
   OUTPUT: ['I', 'AM', 'AKINLADE', 'TEMITOPE']
```
In the above, the (#) is the delimeter which is like informing the program to like hey! cut the string(text) wherever you see the (#). if it was a comma(,) or even a space, that will be your delimeter.


We were give a task to use the split method and the print function using the doctrings format to print multiple lines:
>print(f'''
>Name: Akinlade Temitope
>Discipline: Software Engineering
>''')

on this:
```python=
   # Variable with a value of string to be splitted
   message = "GhostNet#X44CR#98.654#TRC8821"
    
   #OUTPUT
   Code Name:        GhostNet
   Message Code:     X44CR
   Last Letter:      R
   Trace ID:         TRC8821
   Traceable:        Yes
   Severity Level    98.65
```
>[!Note] Using the backward slash and t (\t) to achieve the tab space where necessary in the above output and "message[0]"" to get the first item in a list(datatype) after the string of text has been splitted.

**CONCLUSION**
Being introduced to these concepts and with the hands on tasks was pretty interesting even though it was quite tough in the beginning but repeating the task given in class made it stick so you could dive straight into the above task if you'd like. Thanks for sticking with me this far! See you soon.