Practice the concepts you've learned about strings using the following questions.
Basic (10 Questions)
- Write a Python program to get the first and last character of a string.
- Check whether a string is palindrome or not (e.g., "madam").
- Use
len()
to find the length of any input string.
- Convert a given string to uppercase and lowercase using string methods.
- Concatenate two strings using
+
operator and .format()
.
- Print each character in a string using indexing and a loop.
- Slice a string to get every second character from index 1 to 10.
- Use
count()
to find the number of occurrences of a character in a string.
- Check if a substring is present in a given string using
in
.
- Take a user's name as input and print a greeting using
.format()
.
Intermediate (10 Questions)
- Write a function to reverse a string using slicing.
- Check whether a string contains only digits using built-in functions.
- Replace all spaces in a string with dashes.
- Capitalize the first letter of every word in a sentence using
title()
.
- Compare two strings for equality, ignoring case sensitivity.
- Split a string on a comma and print each element.
- Remove all punctuation from a sentence (hint: use
string.punctuation
).
- Extract the domain from an email address using slicing and
split()
.
- Check if a string starts with a vowel and ends with a consonant.
- Write a function that takes a sentence and returns only the words with more than 4 letters.
Advanced (10 Questions)
- Create a custom string formatting function using
format()
and positional arguments.
- Write a program to find the longest word in a sentence.
- Count the frequency of each character in a string and display as a dictionary.
- Implement a Caesar cipher (basic encryption by shifting characters).
- Write a function to normalize whitespace in a string (multiple spaces to one).
- Generate a unique hash from a string using
hash()
and explain its use case.
- Find all unique substrings of length k in a given string.
- Check if one string is a rotation of another (e.g., "abc" and "cab").
- Implement a string compression algorithm (e.g., "aaabbb" → "a3b3").
- Write a decorator that logs the input and output of any string processing function.