Thread Closed 
Thread Rating:
CHAPTER 6 — STRINGS
#1
Chapter 6 — Strings
Understanding and controlling text in Python

A string is simply text inside quotation marks.

Strings are used for:
• names 
• messages 
• sentences 
• user input 
• filenames 
• any text your program displays 

Python gives you powerful tools to create and manipulate strings.

---

6.1 Creating Strings

Strings always go inside quotes:

Code:
name = "Johno"
colour = "blue"
message = "Welcome to Python!"

You can use:
• double quotes " " 
• or single quotes ' ' 

Both are fine:

Code:
word1 = "hello"
word2 = 'hello'

---

6.2 Printing Strings

Simple:

Code:
print("Hello there!")

Or using variables:

Code:
name = "Mia"
print(name)

---

6.3 Combining Strings (Concatenation)

Use + to join strings:

Code:
first = "Hello"
second = "world"
print(first + " " + second)

Output:
Code:
Hello world

Remember:
You can only add strings to strings.

This fails:

Code:
age = 16
print("Age: " + age)  # ERROR

Fix using str():

Code:
age = 16
print("Age: " + str(age))  # Works

---

6.4 Repeating Strings

Python lets you multiply a string:

Code:
print("ha" * 3)

Output:
Code:
hahaha

Useful for patterns, dividers, or simple banners.

---

6.5 Escape Characters (Special Symbols)

Sometimes you need quotation marks *inside* your string.

Python uses backslashes to escape them:

Code:
print("He said \"Hello!\"")

Common escape codes:
• \n — new line 
• \t — tab 
• \" — quote 
• \\ — backslash 

Example:

Code:
print("Line 1\nLine 2")

---

6.6 String Length

Use len() to measure the number of characters:

Code:
word = "Python"
print(len(word))

Output:
Code:
6

Counting includes:
• spaces 
• letters 
• numbers 
• punctuation 

---

6.7 Indexing — Getting Individual Characters

Each character has a position:

Code:
text = "hello"
print(text[0])  # h
print(text[1])  # e

Important:
• Python starts counting at 0 
• Last character = index -1 

Example:

Code:
print(text[-1])  # o

---

6.8 Slicing — Getting Parts of a String

Format:

Code:
string[start:end]

Example:

Code:
word = "Python"
print(word[0:3])  # Pyt
print(word[2:6])  # thon

Start is included. 
End is NOT included.

Shortcut examples:

Code:
print(word[:3])  # from start to position 3
print(word[3:])  # from position 3 to end

---

6.9 Changing Case

Python has built-in string functions:

Code:
message = "hello world"
print(message.upper())  # HELLO WORLD
print(message.lower())  # hello world
print(message.title())  # Hello World

---

6.10 Removing Spaces

Stripping spaces from ends:

Code:
text = "  hello  "
print(text.strip())  # "hello"

Other options:
• .lstrip() — left only 
• .rstrip() — right only 

---

6.11 Finding Substrings

Check if part of a string exists:

Code:
sentence = "Python is powerful"
print("power" in sentence)      # True
print("weak" in sentence)      # False

---

6.12 Replacing Parts of a String

Code:
text = "I like apples"
new_text = text.replace("apples", "oranges")
print(new_text)

---

6.13 Exercise — Sentence Tools

Create a file called string_tools.py.

Ask the user for a sentence. 
Then print:

1. The length of the sentence 
2. The first character 
3. The last character 
4. The sentence in uppercase 
5. The sentence reversed 

(Hint: reversed string =
Code:
text[::-1]
)

---

6.14 Small Challenge

Ask the user for:
• their first name 
• their last name 

Then output:

Code:
"Initials: F.L."

(Hint: first character of each name)

---

6.15 Chapter Summary

• Strings store text 
• Use + to combine strings 
• Use * to repeat strings 
• Escape characters allow quotes, new lines, etc. 
• len() counts characters 
• Indexing gets single letters 
• Slicing gets parts of a string 
• .upper(), .lower(), .title() change case 
• .strip() removes spaces 
• .replace() replaces text 

Next:
Chapter 7 — Numbers & Maths in Python

Now your programs begin to calculate real values.

---

Written and Compiled by Lee Johnston — Founder of The Lumin Archive
« Next Oldest | Next Newest »
Thread Closed 


Messages In This Thread
CHAPTER 6 — STRINGS - by Leejohnston - 11-15-2025, 10:25 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)