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