Chapter 5: Strings Assignments

Practice the concepts you've learned about strings using the following questions.

Basic (10 Questions)

  1. Write a Python program to get the first and last character of a string.
  2. Check whether a string is palindrome or not (e.g., "madam").
  3. Use len() to find the length of any input string.
  4. Convert a given string to uppercase and lowercase using string methods.
  5. Concatenate two strings using + operator and .format().
  6. Print each character in a string using indexing and a loop.
  7. Slice a string to get every second character from index 1 to 10.
  8. Use count() to find the number of occurrences of a character in a string.
  9. Check if a substring is present in a given string using in.
  10. Take a user's name as input and print a greeting using .format().

Intermediate (10 Questions)

  1. Write a function to reverse a string using slicing.
  2. Check whether a string contains only digits using built-in functions.
  3. Replace all spaces in a string with dashes.
  4. Capitalize the first letter of every word in a sentence using title().
  5. Compare two strings for equality, ignoring case sensitivity.
  6. Split a string on a comma and print each element.
  7. Remove all punctuation from a sentence (hint: use string.punctuation).
  8. Extract the domain from an email address using slicing and split().
  9. Check if a string starts with a vowel and ends with a consonant.
  10. Write a function that takes a sentence and returns only the words with more than 4 letters.

Advanced (10 Questions)

  1. Create a custom string formatting function using format() and positional arguments.
  2. Write a program to find the longest word in a sentence.
  3. Count the frequency of each character in a string and display as a dictionary.
  4. Implement a Caesar cipher (basic encryption by shifting characters).
  5. Write a function to normalize whitespace in a string (multiple spaces to one).
  6. Generate a unique hash from a string using hash() and explain its use case.
  7. Find all unique substrings of length k in a given string.
  8. Check if one string is a rotation of another (e.g., "abc" and "cab").
  9. Implement a string compression algorithm (e.g., "aaabbb" → "a3b3").
  10. Write a decorator that logs the input and output of any string processing function.