Strings: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Create word = "Python" and print its first character. Counting starts at 0, not 1.
Using word = "Python", print its last character using a negative index.
Using word = "Python", print the character at index 2. Work out which letter you expect before running it.
Create a variable holding your favourite programming language and print how many characters it has.
Using word = "Python", print the last character two different ways: with a negative index, and by working it out from len().
Using word = "Python", print the first three characters with a slice.
Print the same first three characters again, but leave the starting 0 out.
Using word = "Python", print everything from index 3 to the end.
Using word = "Python", print the last three characters with a negative slice.
Using word = "Python", print it with the first and last characters removed.
Using word = "Python", print it backwards using a slice with a step of -1.
Using word = "Python", print every second character using a step of 2.
Create text = "Hello World" and print it in all capitals using .upper().
Using the same text, print it in all small letters using .lower().
Create title = "the great gatsby" and print it with every word starting in a capital, using .title().
Using the same title, print it with only the very first letter capitalised, using .capitalize(). Compare it with .title().
Create text = "Hello World" and print it with every letter's case flipped, using .swapcase().
Create messy = " messy input " and print it with the spaces at both ends removed. Add square brackets around the output so you can see the difference.
Using the same messy text, print .lstrip() and .rstrip() separately, with brackets, so you can see which end each one cleans.
Create sentence = "I like cats" and print it with "cats" swapped for "dogs".
Create text = "mississippi" and print how many times "s" appears and how many times "i" appears.
Create sentence = "the quick brown fox" and print the index where "brown" begins, using .find().
Create filename = "report.pdf" and print whether it starts with "report" and whether it ends with ".pdf".
Create sentence = "please review the attached invoice" and print whether the word "invoice" appears anywhere in it.
Print "Ha " repeated three times, then print "Hello " and "there" joined with +.
Using sentence = "the quick brown fox", print the result of .find("zebra") and explain the number you get.
Compare .find() and .index(). Print .find("cat") on a sentence with no cat in it, and explain in a comment what .index("cat") would have done.
Create path = "folder/sub/file.txt" and print the index of the first / and the last /, using .find() and .rfind().
Create text = "one two one two one" and replace only the first two occurrences of "one" with "1".
For each of "12345" and "12a45", print the result of .isdigit().
For each of "abc" and "abc123", print the result of .isalpha().
Print .isalnum() for "abc123", "abc 123" and "". Explain the last two results.
Print .isupper() and .islower() for "HELLO", "hello" and "Hello".
Print .isspace() for " ", "\t\n" and " a ". This is the quick way to spot a blank line.
Print .istitle() for "Hello World" and "Hello world" — a quick check for whether text is already in title case.
Create code = "42" and print it padded with leading zeros to 5 digits, using .zfill().
Create title = "MENU" and print it centred in a 20-wide field padded with -, using .center().
Create name = "Asha" and print it padded to 12 characters using .ljust() and .rjust(), with . as the fill so the width is visible.
Print "Asha" left, centre and right aligned in a 12-wide field using f-string specifiers rather than the padding methods, so you can compare the two approaches.
Build a single string containing a tab between two words, then a newline, then a pair of double quotes. Print it.
Print the Windows path C:\Users\new twice — once with escaped backslashes, once as a raw string (r"..."). Explain why the raw form is safer here.
Create text = "abcdefghij" and print the substring from index 2 up to (but not including) 8, then the same range stepping by 2.
Given stored = "Asha" and typed = "ASHA", print whether they match exactly, and whether they match when case is ignored.
Create text = "xxHelloxx" and print it with the x characters stripped from both ends, by passing them to .strip().
Create record = "name:Riya" and print the result of .partition(":").
Create sentence = "learning python is fun" and print the result of .split().
Create csv_line = "Asha,22,Pune" and split it on commas instead of spaces.
Print the number of words in "the cat sat on the mat" by combining .split() with len().
Using .join(), print the letters of "abc" separated by dashes, so the output reads a-b-c.
Create noisy = " Hello-World ". In one chained expression, strip the outer spaces, swap the - for a space, and lowercase the result.
Ask the user for a username and print a cleaned version: outer spaces removed and everything lowercased.
Ask for a first name and a last name and print a tidy full name with both properly capitalised, however they were typed.
Given phone = "9876543210", print it masked so only the first 2 and last 2 digits show, with * in between.
Ask for a username and print a single boolean that is True only when it is 4–15 characters long and contains no spaces.
Create word = "encyclopedia" and print how many vowels it contains, by counting each vowel and adding the results.
Given email = "student@jrcodex.com", print just the domain part (jrcodex.com), using .find() and a slice.
Using the same email, print just the part before the @.
Given filename = "assignment.final.docx", print just the extension (docx). Use .rfind() so the extra dot in the name doesn't confuse it.
Given full_name = "John Michael Doe", print the initials JMD using .find(), .rfind() and indexing.
Given sentence = "the quick brown fox", print its first word and its last word, using .find(), .rfind() and slicing.
Given sentence = "the quick brown fox", print how many characters it has excluding spaces.
Ask for a filename and print two booleans: whether it is an image (.jpg or .png) and whether it is a document (.pdf or .docx).
Ask for a sentence typed in any case and print it as a proper sentence: first letter capital, everything else small.
Given spaced = "too many spaces", collapse the runs of spaces into single spaces and print the result, using .split() and .join() together.
Ask for a shop name and print it as a banner: a line of =, the name centred in 30 characters, then another line of =.
Ask for an article title and print a URL slug: lowercase, outer spaces stripped, inner spaces turned into hyphens.
Ask for a sentence and print it fully reversed, character by character.
Ask for a 4-digit PIN and print two booleans: whether it is exactly 4 characters, and whether it contains only digits.
Given item = "Notebook" and price = "45.50", print a receipt line with the item padded to 20 characters using dots, and the price right-aligned in 10.
Ask for a sentence and print how many words it has, and how many times the letter e appears in it.
Strings cannot be changed in place. Show that word[0] = "J" would fail, then produce "Jython" from "Python" the correct way.
Show that .upper() does not change the original string: print the original before and after calling it, then show how to actually keep the change.
Create word = "madam" and print whether it is a palindrome by comparing it with its own reverse.
Create word = "Racecar" and print whether it is a palindrome, ignoring capitalisation.
Given phrase = "A man a plan a canal Panama", print whether it is a palindrome once capitalisation and spaces are ignored.
Create word1 = "listen" and word2 = "silent" and print whether they are anagrams.
Given text = "abc", print text[0:99] and text[99:]. Neither crashes — explain how slicing differs from indexing here.
Given text = "abcdefghij", print text[7:2:-1]. Work out the expected answer before running it.
Print "xyxhelloxyx".strip("xy") and explain why the argument is not treated as the word "xy".
Given text = "a b" (two spaces), print len(text.split()) and len(text.split(" ")). They differ — explain why.
Print "aaaa".count("aa") and explain why the answer is 2 rather than 3.
Given sentence = "the theatre is there", print how many times .count("the") finds, and explain why that number is misleading.
Given a = "hello" and b = "hello", print a == b and a is b, and say which one you should be using.
This is meant to print the last character but crashes. Find and fix it.
word = "Python"
print(word[len(word)])This is meant to clean up a messy username, but the output is unchanged. Find and fix the bug.
username = " Asha_2024 "
username.strip()
username.lower()
print(f"[{username}]")Build a Username Generator. Ask for a first name, last name and birth year, then build a username from the first 3 letters of the first name, the first 3 of the last name, and the last 2 digits of the year — all lowercase.
Build a Text Cleaner. Ask for a messy sentence and print the original with markers, the stripped version, the version with inner double spaces collapsed, and the final sentence-cased result.
Build a Palindrome & Anagram Checker. Ask for two words and print whether the first is a palindrome, whether the two are anagrams, and whether they are simply the same word ignoring case.
Build an Email Masker. Ask for an email address and print it with the name part masked except its first character, keeping the domain visible — e.g. s******@jrcodex.com.
Build a Signature Generator. Ask for a full name and a job title, then print a bordered signature block with the name in title case, the job title in capitals, and the initials.
Build a Password Checker. Ask for a password and print flags for: 8 or more characters, no spaces, and contains at least one digit — working the last one out by comparing the length before and after removing every digit.
Build a URL Slug Generator. Ask for an article title and print a report: the original, the collapsed version, the final slug, and its length.
Build a Name Parser. Ask for a three-part full name and print the first, middle and last names on separate labelled lines, using only .find(), .rfind() and slicing.
Build a Card Number Formatter. Ask for a 16-digit card number and print it masked as **** **** **** 1234, plus whether the input really was 16 digits.
Build a Vowel & Consonant Counter. Ask for a word and print the vowel count, the consonant count, and the share of letters that are vowels as a percentage.
Explain string immutability properly: show that a string cannot be edited in place, show that += builds a brand new object, and give one reason Python was designed this way.
Given sentence = "the quick brown fox jumps", print the word count, the character count excluding spaces, and the average word length to 2 decimal places.
Write a single expression that normalises messy user input for comparison — trim, lowercase, and collapse repeated inner spaces — and prove it works by comparing two differently-typed versions of the same name.
Given text = "Was it a car or a cat I saw", print whether it is a palindrome ignoring case and spaces. Show the cleaned string you actually compared.
Build a String Toolkit Report — the fullest demonstration of this topic. Ask for a sentence and print a labelled report containing: its length, the character count without spaces, the word count, the vowel count, upper/lower/title versions, the reverse, the first and last words, whether it is a palindrome ignoring case and spaces, and the sentence centred in a 40-wide banner.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session