Beyond the Basics: Strings

3.1. Beyond the Basics: Strings#

Welcome to the third Notebook. In this Notebook we are going to learn some advanced Python. Let’s first start with strings. Run the code below and see what it prints out.

from math import pi 
import os 
MyFString = f"This is an F-String"
MyString = "This is a string"
print(MyFString)
print(MyString)
This is an F-String
This is a string

Now let’s try inserting some data into our print() function. We’ll use the list of integers [1,2,3,4].

Data = [1,2,3,4]

MyFString = f"Data1: {Data[0]}, Data2: {Data[1]}, Data3: {Data[2]}, Data4: {Data[3]}"

print(MyFString)
print("Data1:",Data[0],",Data2:",Data[1],",Data3:",Data[2],",Data4:",Data[3])
Data1: 1, Data2: 2, Data3: 3, Data4: 4
Data1: 1 ,Data2: 2 ,Data3: 3 ,Data4: 4

As you can see from the above code, it is much easier to insert variables in a string by using an f-string (formatted string).

3.1.1. Formatting numbers#

Using f-strings makes formatting numbers really easy. Just add a colon character after a number value and specify how you want to format the number. The following table demonstrates a couple of examples with the number \(1\):

Code

Result

1:.2f

1.00

1:.0f

1

1:.10f

1.0000000000

1:%

100.000000%

1:.1%

100.0%

1:e

1.000000e+00

As you can see the default number of decimal places is six. Furthermore, the % formatting operator assumes that \(1\) is equal to \(100\)%, which is usual when working with fractions, and the formatting operator e formats using scientific notation.

Now let’s use our newfound knowledge of strings to make a simple progress bar. During other courses, you’ll sometimes have to write algorithms that take a long time to run. In this case, it is useful to have a progress bar. Our example of a progress bar makes use of the sleep() function, from the time module, to simulate elapsed time.

import time

for i in range(11):
    print(f"Loading: {i*10}%", )
    time.sleep(0.5) 
Loading: 0%
Loading: 10%
Loading: 20%
Loading: 30%
Loading: 40%
Loading: 50%
Loading: 60%
Loading: 70%
Loading: 80%
Loading: 90%
Loading: 100%

This works! Though it is not that pretty to look at. It would look nicer to not have it print a new line each time. This is where escape characters come in. These characters can do some special things in strings. Below an example of some escape characters:

Escape characters

Code

Result

'

\

\

\n

new line

\r

carriage return

\t

tab

\b

backspace

We can use some of these characters in our code. Let’s use the carriage return character to make our progress bar not print out a new line every time. We can do this by adding end=”\r” into our print function. The end keyword specifies a string that gets printed at the end. The string we print at the end here is the carriage return character. This carriage resets the print function to the start of the line; thus making the next print function overwrite the current printed line. Try it and see what happens:

print("Will I get overwritten?", end="\r")
print("This is a very important message")
This is a very important message

Now let’s add this to our progress bar…

import time
for i in range(11):
    print(f"Loading: {i*10}%", end="\r")
    time.sleep(0.5) 
print("Loading complete!")
Loading complete!

As you can see, it works beautifully!