Chapter 3 · Data

Working with text

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

Each character has a number

In this lesson you take text apart, make new text from it, and line it up in columns.

word = "Python"
  • Each character has a number, its index. The first is 0.
  • word[0] is "P". Counting from the end, the last is -1: word[-1] is "n".
word = "Python"
print(word[1], word[-1])
word = "Python"
print(len(word))
print(word[6])
word = "Python"
print(word[1:4])
print(word[:3])
print(word[3:])
print(word[::-1])
print(word[3:100])
first = "Ada"
last = "Lovelace"
# 1. print the initials: A.L.
# 2. print the first name and the last initial
word = "java"
word[0] = "J"
print(word)
name = " Ada Lovelace\n"
clean = name.strip()
print(clean.upper())
print(clean.lower().replace(" ", "."))
print(clean.find("L"), clean.count("a"))
print(clean.startswith("Ada"), "42".isdigit())
sentence = "the quick brown fox"
words = sentence.split()
print(words)
print("-".join(words))
print(len(words), "words")
lines = "one\ntwo"
print(lines)
print(lines.split("\n"))
print("Ada\t36")
word = "cat"
for ch in word:
    print(ch)
for w in "big red dog".split():
    print(w, len(w))
print("a" in word, "dog" in word)
def acronym(text):
    # the first letter of each word, in capitals
    return ""


print(acronym("portable network graphics"))
print(acronym("central processing unit"))
title = "Menu"
item = "tea"
qty = 3
big = 1500000
print(f"[{title:^12}]")
print(f"[{item:<8}{qty:>4}]")
print(f"{big:,} cups")
name = "Ada"
typed = "ada"
print(name == typed)
print(name.lower() == typed.lower())
fixed = typed[0].upper() + typed[1:]
print(fixed == name, fixed is name)
# read the name, make the username,
# then print the two lines
word = "python"
secret = ""
# shift each letter 3 places on
print(secret)
Terminal
Step 1 of 13Characters by index

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)