This directory contains exercises to help you practice working with Python strings. Strings are one of the most important data types in Python, with powerful built-in methods and modern formatting capabilities.
- Open your terminal/command prompt
- Navigate to this directory
- Run the exercises:
python strings.py - Or work interactively:
python3 -i strings.py
Write a function get_length(text) that returns the length of a string.
Write a function to_upper_case(text) that converts a string to uppercase.
Write a function to_lower_case(text) that converts a string to lowercase.
Write a function count_vowels(text) that counts vowels (a, e, i, o, u) in a string.
Write a function count_words(text) that counts words in a string (separated by spaces).
Write a function is_palindrome(text) that checks if a string reads the same forwards and backwards.
Write a function find_substring(text, substring) that returns the index of the first occurrence of substring.
Write a function count_occurrences(text, substring) that counts how many times substring appears.
Write a function starts_with_prefix(text, prefix) that checks if text starts with prefix.
Write a function remove_spaces(text) that removes all spaces from a string.
Write a function replace_char(text, old_char, new_char) that replaces all occurrences of old_char with new_char.
Write a function reverse_string(text) that returns the string reversed.
Write a function format_name(first, last) that creates "Last, First" format.
Write a function create_email(username, domain) that creates an email address.
Write a function format_phone(area_code, exchange, number) that formats as "(555) 123-4567".
Write a function is_valid_email(email) that checks basic email format (contains @ and .).
Write a function is_all_digits(text) that checks if string contains only digits.
Write a function is_strong_password(password) that checks if password has uppercase, lowercase, and digits.
Write a function extract_numbers(text) that extracts all numbers from a string and returns them as a list.
Write a function split_csv_line(line) that splits a CSV line into a list of values.
Write a function parse_name(full_name) that splits "First Last" into a tuple (first, last).
Write a function join_with_comma(string_list) that joins strings with commas.
Write a function longest_string(string_list) that finds the longest string in a list.
Write a function filter_by_length(string_list, min_length) that filters strings by minimum length.
Write a function title_case(text) that converts to title case (capitalize each word).
Write a function remove_duplicates(text) that removes duplicate characters while preserving order.
Write a function compress_string(text) that compresses "aabbbcccc" to "a2b3c4".
# Example tests
print(get_length("hello")) # Should return 5
print(count_vowels("hello world")) # Should return 3
print(is_palindrome("racecar")) # Should return True
print(find_substring("hello", "ell")) # Should return 1
print(reverse_string("hello")) # Should return "olleh"
print(format_name("John", "Doe")) # Should return "Doe, John"
print(is_valid_email("test@email.com")) # Should return True
print(extract_numbers("I have 5 cats and 3 dogs")) # Should return [5, 3]- Immutable: Strings cannot be changed, only replaced
- Unicode support: Full support for international characters
- String methods: 40+ built-in methods for manipulation
- Multiple quote styles: Single, double, triple quotes
- f-strings: Modern formatting with f"Hello {name}"
- Raw strings: r"C:\path\to\file" for literal strings
- String multiplication: "ha" * 3 = "hahaha"
- Use f-strings for modern formatting: f"Hello {name}!"
- String methods return new strings (strings are immutable)
- Use
inoperator for substring checking: "ell" in "hello" str.strip()removes whitespace from both endsstr.split()without arguments splits on any whitespacestr.join()is the efficient way to combine many strings