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

Essential String Methods for QA

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"

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}".
πŸ’‘Common doubts
Did you like the problem?
Lesson 3 of 43
Python
Loading code editor…
Next