Text Representations

In computer programming and data structures, a sequence commonly denotes a data type representing an ordered collection of elements. These elements may vary in data types and can be accessed individually by referencing their indices. The order of the elements matters-they are arranged in a specific order, allowing for access based on that order.

In Python, a string is a sequence of characters enclosed in quotes. You can store any ordered character values in a string and access each value by its position. However, it is an immutable data type, meaning that you cannot replace its elements or change the order of the elements.

To create a string object, you can use either single quotes or double quotes. The choice between them can be a matter of personal preference, but according to Google’s Python style guides, it is recommended to use single quotes for single-character strings and double quotes for multi-character strings.

my_string = "Hello, Python!"


To access an element in a string variable, you can use square brackets [] along with a corresponding index. Note that Python indexing starts from 0.

my_string[0] # Output: H
## 'H'


You can also slice a part of a string. The following code will retrieve a substring from my_string starting at index 0 and ending at index 5. The resulting sliced portion will include characters from index 0 upto, but not including, index 5.

my_string[0:5] # Output: Hello
## 'Hello'


If you leave one side of the colon empty in Python slicing, it indicates that you want to retrieve all elements up to (exclusive) or from that point.

my_string[:7]
## 'Hello, '
my_string[7:]
## 'Python!'


Negative indices can also be used for slicing as well. When using negative indices, counting starts from the end of the string, with -1 representing the last character element. This makes sense, considering that the index of the first element is 0.

# Retrieves the last six characters of the string
my_string[-1]
## '!'
# Retrieves the last seven characters of the string
my_string[-7:]
## 'Python!'


The len() function takes a sequence object and returns the number of elements it contains, which we call the length of the object. When applied to a string, it returns the number of consisting characters. For example:

len(my_string)
## 14


You can also slice a string using the following syntax:

string[start:end:step]


Here, start and end are the indices that specify the beginning and end of the desired substring. The step parameter indicates the stride or increment between the characters included in the slice. For example:

my_string[::2] # Starts from 0 to end, every 2nd character
## 'Hlo yhn'
my_string[0:7:2] # Starts from 0 to 7, every 2nd character
## 'Hlo '


Specifying negative value for step retrieves from the end to the start of a string increment by abs(step).

my_string[::-1]
## '!nohtyP ,olleH'
my_string[::-2]
## '!otP,le'


In computer programming, an escape character is a character that signals that the character following it has a special meaning. It is often used to represent characters that are difficult or impossible to type directly. Escape characters are denoted by backslash \ followed by a specific character in Python.

  • \t: tab
  • \n: new line
  • \r: carriage return
  • \b: backspace
  • \f: form feed
  • \v: vertical tab
  • \': single quote
  • \": double quote
  • \\: backslash

For example, the \n represents a newline character, which is a character that causes the cursor to move to the beginning of the next line.

my_haiku = "Pikachu’s bright spark\nA thunderbolt in the dark\nA friend to the end"
print(my_haiku)
## Pikachu’s bright spark
## A thunderbolt in the dark
## A friend to the end


Escape characters can also be used to represent control sequences. A control sequence is a sequence of characters that is used to control the behavior of a terminal or other device. For example, the \r escape character can be used to send a carriage return control sequence, which is a control sequence that causes the cursor to move to the beginning of the current line. For example:

my_haiku = "Electric sparks fly Pikachu dances with joy.\rPython code awakes,"

print(my_haiku)
## Electric sparks fly Pikachu dances with joy.
Python code awakes,


In this example, the \r escape character is used within my_haiku. It represents a carriage return control sequence, causing the cursor to move to the beginning of the current line. As a result, when my_haiku is printed, the cursor moves back to the start of the line and subsequent characters overwrite the initial characters. This leads to the final output where "Python code awakes," replaces the initial part of the haiku.

In Python, triple-quotes ''' denote a multi-line string literals. They allow you to create strings that span multiple lines without using explicit escape characters for line breaks.

my_haiku = '''
Pikachu's spark of life
Turns data into insights bright
Use it for good, friend.
'''

print(my_haiku)
## 
## Pikachu's spark of life
## Turns data into insights bright
## Use it for good, friend.


One of the places where the multi-line strings are particularly useful is documentation of functions, classes, or modules. For example:

def my_function():
   '''
   This function performs a specific task.
   
   Usage:
   my_function()
   
   Returns:
   A meaningful result.
   '''
   
   # Function implementation here


Another place where the multi-line strings are commonly employed is writing SQL queries. When working with RDBMS connected to your Python program, SQL queries can be lengthy and complex. In such scenarios, multi-line strings provide a cleaner way to write and format them within Python code.

sql_query = '''
SELECT
    column1,
    column2
FROM
    my_table
WHERE
    condition = 'value';
'''


The other practical usage of multi-line strings is to embed HTML or XML codes. When generating HTML or XML content dynamically within Python code, it often spans multiple lines. So, you can make it easier to embed these snippets by utilizing multi-line strings.

html_code = '''
<html>
    <head>
        <title>My Website</title>
    </head>
    <body>
        <p>Hello, World!</p>
    </body>
</html>
'''    



Formatting Character Strings

An f-string, short for “formatted string literal,” is a feature introduced in Python 3.6 that provides a concise and convenient way to embed expressions within string literals. F-strings are prefixed with the letter ‘f’ or ‘F’ and allow you to include expressions, variables, and even expressions involving computations directly inside string literals.

Here’s a basic syntax example:

name = "Andrew"
profession = "Data Scientist"

# Using f-string to include variables in the string
info = f"My name is {name}, I work as a {profession}."
print(info)
## My name is Andrew, I work as a Data Scientist.


In this example, the expressions inside the curly braces {} are evaluated and inserted directly into the string. F-strings offer a concise and readable way to format strings without the need for complex concatenation or formatting functions.

Some key features of f-strings include:

Variable Embedding: You can directly embed variable names and expressions inside the string as described earlier. Expressions involving variables within f-strings are dynamically evaluated at runtime.

width = 10
height = 5

area = f"The area is {width * height} square units."
print(area)
## The area is 50 square units.


Expressions and Method Calls: F-strings can include more complex expressions and even method calls.

greeting = f"Hello, {'python'.upper()}!"
print(greeting)
## Hello, PYTHON!


Format Specification: You can include format specifications inside the curly braces to control the appearance of the embedded values.

  • Precision for floating-point numbers:
    • :.2f specifies that a floating-point number should be formatted with two decimal places.
pi = 3.14592

formatted_pi = f"The value of pi is approximately {pi:.2f}."
print(formatted_pi)
## The value of pi is approximately 3.15.


  • Width and alignment:
    • {:<10} aligns the value to the left with a width of 10 characters.
    • {^10} centers the value within a width of 10 characters.
    • {:>10} aligns the value to the right within a width of 10 characters.
name = "Andrew"

aligned_name = f"{name:<10} | {name:^10} | {name:>10}"
print(aligned_name)
## Andrew     |   Andrew   |     Andrew


  • Leading zeros:
    • {:05d} pads an integer with leading zeros to achieve a width of 5 digits.
number = 42
padded_number = f"The padded number is: {number:05d}."

print(padded_number)
## The padded number is: 00042.


  • Commas as Thousands Separators:
    • {:,} adds commas as thousands separators for integers.
population = 341814420
formatted_population = f"The population is: {population:,}"

print(formatted_population)
## The population is: 341,814,420


  • Binary, octal, and hexadecimal representation:
    • {:b} formats an integer as binary.
    • {:o} formats an integer as octal.
    • {:x} or {:X} formats an integer as hexadecimal (lowercase or uppercase).
value = 42
binary_representation = f"Binary: {value:b}, Octal: {value:o}, Hex: {value:x}."

print(binary_representation)
## Binary: 101010, Octal: 52, Hex: 2a.


These format specifications provide fine-grained control over the appearance of the values, allowing you to format numbers, align text, and achieve various other formatting effects within your strings. Using format specifications enhances the versatility of f-strings, making them powerful tools for creating well-formatted output in Python.

Post a Comment

0 Comments