Python 9a – String Handling

Work with text: change case, count letters, measure length, slice/reverse, split into words—and practise with tasks.

Case conversion

Use .upper() and .lower() to normalise text for comparisons:

breed = "Beagle"
print(breed.upper())   # BEAGLE

nickname = "SparKy"
print(nickname.lower())  # sparky

response = input("Do you want water? ")
if response.lower() == "yes":
    print("I'll get you a glass.")
else:
    print("Alright, stay hydrated!")

Counting characters

Use .count(substring) to count occurrences (case-sensitive unless you normalise first):

text = input("Type a sentence: ")
count_e = text.count("e")
print("The letter 'e' appears", count_e, "times.")

Measuring string length

len(string) returns the number of characters—useful for validation:

fruit = input("Enter a fruit name: ")
print("The name has", len(fruit), "characters.")

Checking start/end

Use .startswith() and .endswith() for pattern checks:

country = input("Enter a country: ")
if country.endswith("land"):
    print("You've entered a country ending with 'land'.")
elif country.endswith("stan"):
    print("You've entered a country ending with 'stan'.")
else:
    print("Thanks for your entry.")

Reversing text

Slice with a step of -1 to reverse:

user_text = input("Enter a sentence: ")
print("Reversed:", user_text[::-1])

Splitting into words

.split() breaks a string into a list (default: spaces). Then analyse words:

sentence = input("Enter a sentence: ")
words = sentence.split()
print("Total words:", len(words))

s_count = 0
for word in words:
    if word.lower().startswith("s"):
        s_count += 1
print("Words starting with 's':", s_count)
Tips:
  • Normalise with .strip() to remove leading/trailing spaces.
  • For case-insensitive searches, compare on text.lower().
  • Use slicing: text[a:b] for substrings; omit ends to go from start or to end.

Practice tasks

Task 1: Format a name

Ask for first name and surname; show SURNAME, first.

Show answer
first_name = input("Enter your first name: ").strip()
surname = input("Enter your surname: ").strip()
formatted = surname.upper() + ", " + first_name.lower()
print("Welcome", formatted)
Task 2: Count the letter "a"

Count both uppercase and lowercase a in a sentence.

Show answer
sentence = input("Enter a sentence: ")
a_count = sentence.lower().count("a")
print("The letter 'a' appears", a_count, "times.")
Task 3: Validate name length

Check if a name’s length is between 4 and 10 characters inclusive.

Show answer
name = input("Enter a name: ")
if 4 <= len(name) <= 10:
    print("That name is just right!")
else:
    print("The name must be between 4 and 10 characters.")
Task 4: Middle character

Input a long word and print the middle character.

Show answer
word = input("Enter a long word: ")
mid = len(word) // 2
print("The middle character is:", word[mid])
Task 5: Word counter

Show the total number of words and how many start with s.

Show answer
sentence = input("Enter a sentence: ")
word_list = sentence.split()
total = len(word_list)
s_words = sum(1 for w in word_list if w.lower().startswith("s"))
print("Total words:", total)
print("Words starting with 's':", s_words)