Python Strings | 3/100 Days of Python Algo trading

Python Strings

Day 3: Python Strings – Basic Foundations for Algorithmic Trading

Welcome to Day 3 of 100 Days of Hell with Python Algo Trading! Today we will be focusing on one of the most fundamental data types in Python – Strings. As the saying goes, “The devil is in the details”. Strong basics build a strong foundation for you when you are engaged in quantitative trading or crypto trading strategies. Let us learn about Python Strings in detail, understand their encoding (ASCII vs Unicode), creation, indexing, slicing and other important operations that are extremely useful in algorithmic trading.

What are Python Strings?

  • Python Strings are a sequence of characters. In Python, Strings are actually sequences of Unicode characters.
  • Unicode is a comprehensive encoding standard that supports all the writing systems, symbols, emojis and control characters in the world.
  • The old ASCII (American Standard Code for Information Interchange) had only 128 characters, mainly limited to the English alphabet and some control characters.
  • So, Python uses Unicode which lets you easily handle characters from different languages.

Unicode vs ASCII

ASCII:

  • Limited: only 128 characters.
  • Use: mainly for the English language.
  • Fixed size: 7-bit encoding.

Unicode:

  • Huge: millions of possible characters.
  • Use: for all languages, symbols, emojis, etc.
  • Variable size: with different encoding formats like UTF-8, UTF-16, and UTF-32.

It is important to understand these differences because when you process global data, you need the power of Unicode.

Python Strings Creation and Operations

1. String Creation:
You can use single quotes (‘ ‘), double quotes (” “) or triple quotes (“”” “”” or ”’ ”’) to create strings in Python.

Examples:

Single-line String:

strategy_name = “Moving Crossover Strategy”

Multi-line String:

description = “””Hello Algo Traders,
We are here to learn algorithmic trading with Python.

Let’s dive deep into the world of trading strategies.”””

When your string has single quotes inside it, you can use double quotes so that there is no confusion.

2. Indexing in Strings:

  • Strings in Python are accessed via index.
  • Positive Indexing: The first character is at index 0.
  • Negative Indexing: The last character is accessed at -1.

Example:

s = “Moving Crossover Strategy”

print(s[0]) # Output: M (first character)

print(s[-1]) # Output: y (last character)

This method helps you to extract special characters from any large string.

3. Slicing in Strings:

Using slicing, you can extract a part of a string.

Syntax: string[start:stop:step]

Example:

s = “Moving Crossover Strategy”

print(s[0:6]) # Output: Moving

print(s[7:16]) # Output: Crossover

Slicing allows you to easily extract substrings, which is useful for data processing and trading signals.

4. String Operations:

Many operations can be performed on strings in Python:

Concatenation: Joining two strings

greeting = “Hello, ” + “Trader!”

Repetition: Repeating a string repeatedly

repeat = “Hi! ” * 3

Membership Testing: Presence of a character or substring

“Crossover” in s # Output: True

With these operations, you can easily manage text-based trading alerts, logs, and data feeds.

Importance of Python Strings in Algo Trading
In quantitative traders and crypto trading strategies, Strings are used to manage different types of data:

  • Trade Logs: Text logs to keep trading records.
  • Configuration Files: Storing settings and parameters in formats like JSON, YAML, etc.
  • Data Parsing: Processing API responses and market data.

Understanding Python Strings is extremely important for all this.

Watch this Day 3 video tutorial

Day 3: Python Strings

1. Consider the code: my_string = “Summer 2024”. Which of the following expressions would correctly extract the year?

2. What is the output of the following code?
Python
text = “Python Programming”new_text = text.replace(“Programming”, “Finance”)
print(new_text)

3. What is the purpose of the following code?
Python
ticker = ”  AAPL  “clean_ticker = ticker.strip()

4. Which code correctly checks if a string starts with a specific substring?
Python
message = “Welcome to the course”

5. How would you split the string portfolio = “AAPL-MSFT-GOOG” into a list of individual ticker symbols using the hyphen as the separator?

6. What is the output of the following code?
Python
name = “Jane”age = 30info = f”Name: {name}, Age: {age}print(info)

7. What data type does the .split() method return?

8. How would you convert a list of strings into a single string joined by commas?

9. Which method would you use to find the first occurrence of the letter “o” in the string text = “coding”?

10. What does the isalnum() string method do?

11. You have a string price = “$123.45”. How would you remove the dollar sign?

12. What is the output of the following code?

Python
text = “Coding is fun”result = text[::-1]
print(result)

13. How do you check if a string is entirely lowercase?

14. What is the difference between string.find() and string.index()?

15. How would you format a floating-point number to two decimal places in a string?

16. What does the isdigit() string method do?

17. How would you convert the string my_string = “hello world” to title case (first letter of each word capitalized)?

18. What is the purpose of the rfind() string method?

19. Which method reverses the order of items in a list?

20. Given the string text = ” Hello, world! “, what is the result of text.strip().split(“,”)?