Skip to content

Latest commit

 

History

History
86 lines (71 loc) · 4.41 KB

File metadata and controls

86 lines (71 loc) · 4.41 KB

String Exercises

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.

Setup

  1. Open your terminal/command prompt
  2. Navigate to this directory
  3. Run the exercises: python strings.py
  4. Or work interactively: python3 -i strings.py

Exercises

Exercise 1: Basic String Operations

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.

Exercise 2: String Analysis

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.

Exercise 3: String Searching

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.

Exercise 4: String Modification

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.

Exercise 5: String Formatting

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".

Exercise 6: String Validation

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.

Exercise 7: String Parsing

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).

Exercise 8: String Lists

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.

Exercise 9: Advanced String Processing

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".

Testing Your Code

# 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]

Python String Features

  • 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"

Tips

  • Use f-strings for modern formatting: f"Hello {name}!"
  • String methods return new strings (strings are immutable)
  • Use in operator for substring checking: "ell" in "hello"
  • str.strip() removes whitespace from both ends
  • str.split() without arguments splits on any whitespace
  • str.join() is the efficient way to combine many strings