3.1. Beyond the Basics: Strings#
In Python, strings
are created using quotes (’’ or “”) and are immutable, while f-strings
are formatted strings that allow embedding expressions inside curly braces { } for dynamic value substitution during runtime. Here is a couple of examples for strings:
from math import pi
import os
name = "Alice"
age = 25
profession = "engineer"
print (name,age,profession)
Alice 25 engineer
Here is the same example made as a complete sentence using f-strings
.
intro = f"My name is {name}, I'm {age} years old, and I work as an {profession}."
print (intro)
My name is Alice, I'm 25 years old, and I work as an engineer.
3.1.1. Formatting numbers#
Python’s f-strings provide a convenient way to format numbers by using the colon character and specifying the desired format. The following table demonstrates a couple of examples with the number \(1\) using f-strings
:
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 |
In the example below, the sleep() function from the time module is used to simulate the passage of time and provide a simple demonstration of a progress bar implementation. We define a function simulate_long_running_algorithm() that performs a loop with a sleep of 0.5 seconds between iterations. Within each iteration, the progress of the algorithm is calculated and used to construct a progress bar string. The progress bar consists of a series of equal signs (=) that visually represent the progress, followed by the percentage completion formatted to one decimal defined inside an f-strings
.
import time
def simulate_long_running_algorithm():
total_iterations = 10
for i in range(total_iterations):
time.sleep(0.5) # Simulating processing time
progress = (i + 1) / total_iterations
progress_bar = f"[{'=' * int(progress * 20):20s}] {progress * 100:.1f}%"
print(progress_bar, end='\r') # Print on the same line
print("\nAlgorithm complete!")
simulate_long_running_algorithm()
[====================] 100.0%
Algorithm complete!
Escape characters
Escape characters
in programming are special characters that are used to represent certain non-printable or special characters within strings. They are typically represented by a backslash (’ \ ‘) followed by a specific character or sequence. We used two escape characters
in the previous example, can you identify them?
Code |
Result |
Description |
---|---|---|
' |
‘ |
represents the escape sequence for a single quote (‘). |
\ |
\ |
represents the escape sequence for a backslash (’ \ ‘). |
\n |
new line |
represents the escape sequence for a new line character, which moves the cursor to the beginning of the next line. |
\r |
carriage return |
represents the escape sequence for a carriage return character, which moves the cursor to the beginning of the current line. |
\t |
tab |
represents the escape sequence for a tab character, which adds horizontal spacing. |
\b |
backspace |
represents the escape sequence for a backspace character, which moves the cursor one position back. |