Selenium + Python · Lesson 3 of 43

0/43 done

Module 3: Strings

3. String Methods, Text Slicing & Modern f-strings

14 minlesson

Your task

Implement clean_and_extract_order(text):

  1. Strip leading and trailing whitespace.
  2. If text starts with "ORD-", return text.
  3. Otherwise return f"ORD-{text}".

Write it in the editor, then press Check answer (Ctrl + Enter).

Essential String Methods for QA

python
raw_text = "   Order #49281 Confirmed!   "

# Cleaning and extracting:
cleaned = raw_text.strip() # "Order #49281 Confirmed!"
is_order = cleaned.startswith("Order #")
order_id = cleaned.split("#")[1].split()[0] # "49281"

# Formatting with f-strings:
env = "staging"
login_url = f"https://{env}.targetqa.com/login"

Key Takeaways & Walkthrough Summary

Every web page contains text: button labels, order IDs, and error banners. In this lesson, you will master Python string methods like strip(), split(), replace(), and f-strings to format and parse test data. Let's master strings!

Part of the Selenium + Python course (Selenium with Python). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

What is the advantage of f-strings over .format()?

f-strings (Python 3.6+) are faster, more readable, and evaluate expressions directly inside {expression}.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip