Sling Academy
Home/Python/The modern Python strings cheat sheet

The modern Python strings cheat sheet

Last updated: May 24, 2023

This page provides a comprehensive, example-style cheat sheet about strings in modern Python (Python 3.10 and newer versions). I also use it quite often for quick lookups when building projects. You can bookmark it as a handy reference for later use.

Creating strings

name = "Ranni the Witch"
message = 'Welcome to Sling Academy'

String length

text = "Hello"
length = len(text)

Accessing characters

text = "Hello"
first_char = text[0] # H
last_char = text[-1] # o

String slicing

text = "Hello, World!"
substring = text[7:12]  # Output: "World"

String concatenation

greeting = "Hello"
name = "Wolf"
message = greeting + ", " + name + "!"  # Output: "Hello, Wolf!"

String case conversion (lower case & upper case)

text = "Hello, World!"
upper_case = text.upper()  # Output: "HELLO, WORLD!"
lower_case = text.lower()  # Output: "hello, world!"

String Formatting with f-strings

name = "Wolf"
age = 99
message = f"My name is {name} and I am {age} years old."  # Output: "My name is Wolf and I am 99 years old."

String searching

text = "Hello, World!"
contains_hello = "Hello" in text  # Output: True
index_hello = text.index("Hello")  # Output: 0

String splitting

text = "Welcome to slingacademy.com!"

# split the text by space
words = text.split(" ") 
# Output: ['Welcome', 'to', 'slingacademy.com!']

String stripping

text = "   Hello, World!   "
stripped_text = text.strip()  # Output: "Hello, World!"

String reversed

text = "Hello"
reversed_text = text[::-1]  # Output: "olleH"

Remove prefixes and remove suffixes

text = "Hello, World!"
removed_prefix = text.removeprefix("Hello, ")  # Output: "World!"
removed_suffix = text.removesuffix("!")  # Output: "Hello, World"

String replacing

text = "Hello, World!"
new_text = text.replace("Hello", "Hi")  # Output: "Hi, World!"

String alignment

text = "Hello"
left_aligned = text.ljust(10)  # Output: "Hello     "
right_aligned = text.rjust(10)  # Output: "     Hello"
centered = text.center(10)  # Output: "  Hello   "

String partitioning

text = "Hello, World!"
partitioned = text.partition(",")  # Output: ("Hello", ",", " World!")
rpartitioned = text.rpartition(" ")  # Output: ("Hello,"," ","World!")

Next Article: Python String Slicing: Tutorial & Examples

Series: Working with Strings in Python

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots