Hintor
Python Basics for Programmers

Working with Python Strings

Python for Developers/Python Basics for Programmers/

Working with Python Strings

Python

Lesson explanation

Working with Python Strings


🎯 Learning Objective

After this lesson, you will be able to:

  • Choose readable string quotes and create multi-line text.
  • Read string length and extract characters or slices.
  • Clean and normalize text with common string methods.
  • Split text into parts and join those parts into a new string.

Short Explanation

Python strings can use single quotes, double quotes, or triple quotes. Choose the quote style that keeps the text readable; triple-quoted strings are useful when the value itself spans several lines.

A string is an ordered sequence of characters. Indexing reads one character, slicing reads a range, and len() returns the number of characters:

Python
code = "API-204"
first = code[0]
number = code[4:]

Strings are immutable. Methods such as strip(), lower(), upper(), and replace() return new strings instead of modifying the original value.

split() turns one string into parts, while join() combines string parts using a chosen separator. This pair is useful for commands, routes, tags, records, and imported text.

💡 Tip: Calling split() with no argument treats repeated whitespace as one separator, which is useful for messy human-entered text.


Clean Code Examples

1. Choose quotes that keep text readable

Use a different outer quote when the text already contains quotation marks. Triple quotes preserve multiple lines.

Python
event = 'Service "billing" restarted'
notice = """Maintenance window
Starts at 02:00 UTC"""

print(event)
print(notice)

Output:

TEXT
Service "billing" restarted
Maintenance window
Starts at 02:00 UTC

2. Read fixed parts of a string

Indexes start at 0. Negative indexes count from the end, and slices exclude the ending position.

Python
ticket = "INC-4821"

print(ticket[:3])
print(ticket[-4:])
print(len(ticket))

Output:

TEXT
INC
4821
8

3. Clean and normalize a value

String methods return a new value, so store or use the result.

Python
raw_status = "  Pending Review  "
clean_status = raw_status.strip().upper()

print(clean_status)

Output:

TEXT
PENDING REVIEW

4. Split a record and join it differently

Split using the input separator, then join the parts using the desired output separator.

Python
record = "api,worker,scheduler"
parts = record.split(",")

print(" | ".join(parts))

Output:

TEXT
api | worker | scheduler


⚠️ Common Mistakes

  1. Do not expect a string method to modify the original string in place.
  2. Do not assume strip() removes repeated whitespace from the middle.
  3. Do not forget that a slice stops before its ending index.
  4. Do not split with one separator when the input actually uses another.
Start Practice

Python for Developers

Working with Python Strings

4 tasks
Lesson progress0 / 4 tasks completed

Tasks

Practice progress

Clean a copied deployment command

View task

Complete the cleanup so the copied command no longer contains outer spaces.

Requirements

  • Fill only the missing method name.
  • Keep the command text unchanged.
  • Print exactly the expected output.

Expected Output

TEXT
deploy --safe

Expected output

deploy --safe
Code: PythonTask 1

Clean a copied deployment command

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 4 tasks completed

Previous lessonBack to unitBack to track
GoalPython
Multi-line string"""line one\nline two"""
String lengthlen(text)
First charactertext[0]
Slice from index 4 onwardtext[4:]
Remove outer whitespacetext.strip()
Normalize casetext.lower() / text.upper()
Replace texttext.replace(old, new)
Split into partstext.split(separator)
Join string partsseparator.join(parts)