Sling Academy
Home/Python/Working with Raw Strings in Python

Working with Raw Strings in Python

Last updated: May 28, 2023

Quick Overview

In Python, raw strings are string literals prefixed with the letter r. They are used to treat backslashes (/) as literal characters, ignoring their escape sequence interpretation. Raw strings are commonly used when working with regular expressions, file paths, or any other scenario where backslashes need to be treated as regular characters.

Examples

Some real-world code examples demonstrate the usefulness of raw strings in Python.

File Path

Use a raw string to represent a file path:

file_path = r"C:\Users\Sling Academy\Documents\file.txt"
print(file_path)

Output:

C:\Users\Sling Academy\Documents\file.txt

Raw Strings and Regular Expressions

Using a raw string in a regular expression pattern ensures that backslashes are treated as literal characters, allowing you to write the pattern without additional escaping. This simplifies and increases the readability of your code.

In the example below, the raw string r"\b[A-Z]+\b" is a regular expression pattern used to match uppercase words in a given text:

import re

# Find all the words that start with a capital letter
pattern = r"\b[A-Z]+\b"
text = "Welcome to SLING ACADEMY. Happy coding!"

matches = re.findall(pattern, text)
print(matches)

Output:

['SLING', 'ACADEMY']

Preserving Double Backslashes as Literal Characters

In the following example, we use a raw string to preserve double backslashes as literal characters:

raw_string = r"This is a raw string with \\ escaped backslashes."
regular_string = "This is a regular string with \\\\ escaped backslashes."

print(raw_string)
print(regular_string)

Output:

This is a raw string with \\ escaped backslashes.
This is a regular string with \\ escaped backslashes.

Next Article: Python: 4 Ways to Remove Substrings from a String

Previous Article: Python: 4 Ways to Count Words in a String

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