Python · Python Fundamentals

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.

Q1IndexingEasyMust Do

Create word = "Python" and print its first character. Counting starts at 0, not 1.

Q2IndexingEasy

Using word = "Python", print its last character using a negative index.

Q3IndexingEasy

Using word = "Python", print the character at index 2. Work out which letter you expect before running it.

Q4len()EasyMust Do

Create a variable holding your favourite programming language and print how many characters it has.

Q5IndexingEasy

Using word = "Python", print the last character two different ways: with a negative index, and by working it out from len().

Q6SlicingEasyMust Do

Using word = "Python", print the first three characters with a slice.

Q7SlicingEasy

Print the same first three characters again, but leave the starting 0 out.

Q8SlicingEasy

Using word = "Python", print everything from index 3 to the end.

Q9SlicingEasy

Using word = "Python", print the last three characters with a negative slice.

Q10SlicingEasy

Using word = "Python", print it with the first and last characters removed.

Q11SlicingEasyMust Do

Using word = "Python", print it backwards using a slice with a step of -1.

Q12SlicingEasy

Using word = "Python", print every second character using a step of 2.

Q13Case MethodsEasyMust Do

Create text = "Hello World" and print it in all capitals using .upper().

Q14Case MethodsEasy

Using the same text, print it in all small letters using .lower().

Q15Case MethodsEasy

Create title = "the great gatsby" and print it with every word starting in a capital, using .title().

Q16Case MethodsEasy

Using the same title, print it with only the very first letter capitalised, using .capitalize(). Compare it with .title().

Q17Case MethodsEasy

Create text = "Hello World" and print it with every letter's case flipped, using .swapcase().

Q18Whitespace MethodsEasyMust Do

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.

Q19Whitespace MethodsEasy

Using the same messy text, print .lstrip() and .rstrip() separately, with brackets, so you can see which end each one cleans.

Q20ReplacingEasyMust Do

Create sentence = "I like cats" and print it with "cats" swapped for "dogs".

Q21CountingEasy

Create text = "mississippi" and print how many times "s" appears and how many times "i" appears.

Q22SearchingEasyMust Do

Create sentence = "the quick brown fox" and print the index where "brown" begins, using .find().

Q23SearchingEasy

Create filename = "report.pdf" and print whether it starts with "report" and whether it ends with ".pdf".

Q24MembershipEasy

Create sentence = "please review the attached invoice" and print whether the word "invoice" appears anywhere in it.

Q25ConcatenationEasy

Print "Ha " repeated three times, then print "Hello " and "there" joined with +.

Q26SearchingMedium

Using sentence = "the quick brown fox", print the result of .find("zebra") and explain the number you get.

Q27SearchingMedium

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.

Q28SearchingMedium

Create path = "folder/sub/file.txt" and print the index of the first / and the last /, using .find() and .rfind().

Q29ReplacingMedium

Create text = "one two one two one" and replace only the first two occurrences of "one" with "1".

Q30Validation MethodsMediumMust Do

For each of "12345" and "12a45", print the result of .isdigit().

Q31Validation MethodsMedium

For each of "abc" and "abc123", print the result of .isalpha().

Q32Validation MethodsMedium

Print .isalnum() for "abc123", "abc 123" and "". Explain the last two results.

Q33Validation MethodsMedium

Print .isupper() and .islower() for "HELLO", "hello" and "Hello".

Q34Validation MethodsMedium

Print .isspace() for " ", "\t\n" and " a ". This is the quick way to spot a blank line.

Q35Validation MethodsMedium

Print .istitle() for "Hello World" and "Hello world" — a quick check for whether text is already in title case.

Q36PaddingMedium

Create code = "42" and print it padded with leading zeros to 5 digits, using .zfill().

Q37PaddingMediumMust Do

Create title = "MENU" and print it centred in a 20-wide field padded with -, using .center().

Q38PaddingMedium

Create name = "Asha" and print it padded to 12 characters using .ljust() and .rjust(), with . as the fill so the width is visible.

Q39FormattingMediumMust Do

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.

Q40Escape SequencesMedium

Build a single string containing a tab between two words, then a newline, then a pair of double quotes. Print it.

Q41Escape SequencesMedium

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.

Q42SlicingMedium

Create text = "abcdefghij" and print the substring from index 2 up to (but not including) 8, then the same range stepping by 2.

Q43Case MethodsMedium

Given stored = "Asha" and typed = "ASHA", print whether they match exactly, and whether they match when case is ignored.

Q44Whitespace MethodsMedium

Create text = "xxHelloxx" and print it with the x characters stripped from both ends, by passing them to .strip().

Q45SplittingMedium

Create record = "name:Riya" and print the result of .partition(":").

Q46SplittingMediumMust Do

Create sentence = "learning python is fun" and print the result of .split().

Q47SplittingMedium

Create csv_line = "Asha,22,Pune" and split it on commas instead of spaces.

Q48SplittingMedium

Print the number of words in "the cat sat on the mat" by combining .split() with len().

Q49JoiningMediumMust Do

Using .join(), print the letters of "abc" separated by dashes, so the output reads a-b-c.

Q50Text ProcessingMediumMust Do

Create noisy = " Hello-World ". In one chained expression, strip the outer spaces, swap the - for a space, and lowercase the result.

Q51Real-WorldMediumMust Do

Ask the user for a username and print a cleaned version: outer spaces removed and everything lowercased.

Q52Real-WorldMedium

Ask for a first name and a last name and print a tidy full name with both properly capitalised, however they were typed.

Q53Real-WorldMedium

Given phone = "9876543210", print it masked so only the first 2 and last 2 digits show, with * in between.

Q54Real-WorldMedium

Ask for a username and print a single boolean that is True only when it is 4–15 characters long and contains no spaces.

Q55CountingMediumMust Do

Create word = "encyclopedia" and print how many vowels it contains, by counting each vowel and adding the results.

Q56Text ProcessingMediumMust Do

Given email = "student@jrcodex.com", print just the domain part (jrcodex.com), using .find() and a slice.

Q57Text ProcessingMedium

Using the same email, print just the part before the @.

Q58Text ProcessingMedium

Given filename = "assignment.final.docx", print just the extension (docx). Use .rfind() so the extra dot in the name doesn't confuse it.

Q59Text ProcessingMedium

Given full_name = "John Michael Doe", print the initials JMD using .find(), .rfind() and indexing.

Q60Text ProcessingMedium

Given sentence = "the quick brown fox", print its first word and its last word, using .find(), .rfind() and slicing.

Q61CountingMedium

Given sentence = "the quick brown fox", print how many characters it has excluding spaces.

Q62Real-WorldMedium

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

Q63Real-WorldMediumMust Do

Ask for a sentence typed in any case and print it as a proper sentence: first letter capital, everything else small.

Q64Text ProcessingMedium

Given spaced = "too many spaces", collapse the runs of spaces into single spaces and print the result, using .split() and .join() together.

Q65FormattingMedium

Ask for a shop name and print it as a banner: a line of =, the name centred in 30 characters, then another line of =.

Q66Text ProcessingMedium

Ask for an article title and print a URL slug: lowercase, outer spaces stripped, inner spaces turned into hyphens.

Q67SlicingMedium

Ask for a sentence and print it fully reversed, character by character.

Q68Real-WorldMedium

Ask for a 4-digit PIN and print two booleans: whether it is exactly 4 characters, and whether it contains only digits.

Q69FormattingMediumMust Do

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.

Q70Real-WorldMedium

Ask for a sentence and print how many words it has, and how many times the letter e appears in it.

Q71ImmutabilityHardMust Do

Strings cannot be changed in place. Show that word[0] = "J" would fail, then produce "Jython" from "Python" the correct way.

Q72ImmutabilityHard

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.

Q73PalindromeHardMust Do

Create word = "madam" and print whether it is a palindrome by comparing it with its own reverse.

Q74PalindromeHard

Create word = "Racecar" and print whether it is a palindrome, ignoring capitalisation.

Q75PalindromeHard

Given phrase = "A man a plan a canal Panama", print whether it is a palindrome once capitalisation and spaces are ignored.

Q76AnagramHardMust Do

Create word1 = "listen" and word2 = "silent" and print whether they are anagrams.

Q77SlicingHard

Given text = "abc", print text[0:99] and text[99:]. Neither crashes — explain how slicing differs from indexing here.

Q78SlicingHard

Given text = "abcdefghij", print text[7:2:-1]. Work out the expected answer before running it.

Q79Whitespace MethodsHard

Print "xyxhelloxyx".strip("xy") and explain why the argument is not treated as the word "xy".

Q80SplittingHard

Given text = "a b" (two spaces), print len(text.split()) and len(text.split(" ")). They differ — explain why.

Q81CountingHard

Print "aaaa".count("aa") and explain why the answer is 2 rather than 3.

Q82SearchingHard

Given sentence = "the theatre is there", print how many times .count("the") finds, and explain why that number is misleading.

Q83ComparisonHard

Given a = "hello" and b = "hello", print a == b and a is b, and say which one you should be using.

Q84DebuggingHard

This is meant to print the last character but crashes. Find and fix it.

word = "Python"
print(word[len(word)])
Q85DebuggingHard

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}]")
Q86Mini-ProjectMini-ProjectMust Do

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.

Q87Mini-ProjectMini-Project

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.

Q88Mini-ProjectMini-ProjectMust Do

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.

Q89Mini-ProjectMini-Project

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.

Q90Mini-ProjectMini-Project

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.

Q91Mini-ProjectMini-Project

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.

Q92Mini-ProjectMini-Project

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.

Q93Mini-ProjectMini-Project

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.

Q94Mini-ProjectMini-Project

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.

Q95Mini-ProjectMini-Project

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.

Q96InterviewInterview

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.

Q97InterviewInterview

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.

Q98InterviewInterview

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.

Q99InterviewInterview

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.

Q100CapstoneInterviewMust Do

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