Python · Python Fundamentals

Strings — Practice Questions

50 questions. Try each one yourself before checking the answer.

Q1IndexingEasy

Create word = "Python". Print its first character using indexing.

Q2IndexingEasy

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

Q3IndexingEasy

Using word = "Python", print the character at index 2.

Q4len()Easy

Create a string with your favorite programming language's name and print its length using len().

Q5SlicingEasy

Using word = "Python", print the first three characters using slicing.

Q6SlicingEasy

Using word = "Python", print the last three characters using slicing.

Q7SlicingEasy

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

Q8String MethodsEasy

Create text = "Hello World". Print it in all uppercase using .upper() and all lowercase using .lower().

Q9String MethodsEasy

Create text = " messy input ". Print it with leading and trailing whitespace removed using .strip().

Q10String MethodsEasy

Create title = "the great gatsby". Print it with each word capitalized using .title().

Q11SlicingMedium

Using word = "Python", print the string reversed using slicing with a step of -1.

Q12SlicingMedium

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

Q13String MethodsMedium

Create sentence = "I like cats". Replace "cats" with "dogs" using .replace() and print the result.

Q14SearchingMedium

Create sentence = "the quick brown fox". Use .find() to print the index where "brown" starts, and the result of searching for a word that isn't there.

Q15CountingMedium

Create text = "mississippi". Print how many times "s" and "i" each appear, using .count().

Q16String MethodsMedium

Create sentence = "learning python is fun". Split it into a list of words using .split() and print the result.

Q17String MethodsMedium

Create a list words = ["learning", "python", "is", "fun"]. Join them back into a single sentence separated by spaces using .join().

Q18String MethodsMedium

Create filename = "report.pdf". Print whether it .startswith("report") and whether it .endswith(".pdf").

Q19String MethodsMedium

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

Q20String MethodsMedium

Create text = "hello WORLD". Print the result of .capitalize() and .title() side by side and explain the difference in a comment.

Q21Real-WorldMedium

Ask the user for a username, then print a "cleaned" version: whitespace stripped and lowercased.

Q22Real-WorldMedium

Ask for a first name and last name, then print a nicely formatted full name using .title() on each.

Q23Real-WorldMedium

Given phone = "9876543210", mask it so only the first 2 and last 2 digits are visible, replacing the middle with * (e.g. 98******10), using slicing and string multiplication.

Q24Real-WorldMedium

Ask the user for a username and print is_valid, True only if it's between 4 and 15 characters and made up entirely of letters/digits (.isalnum()).

Q25CountingMedium

Create word = "encyclopedia". Count the total number of vowels (a, e, i, o, u) it contains by summing individual .count() calls.

Q26Text ProcessingMedium

Create email = "student@jrcodex.com". Extract and print just the domain (jrcodex.com) using .split("@") and indexing.

Q27Text ProcessingMedium

Create filename = "assignment.final.docx". Extract and print just the file extension (docx) using .split(".") and indexing.

Q28Text ProcessingMedium

Ask for a full name (e.g. "John Michael Doe") and print the initials (JMD) by splitting on spaces and taking the first character of each part.

Q29MembershipMedium

Create sentence = "please review the attached invoice". Print whether the word "invoice" appears in it, using in.

Q30FormattingMedium

Create title = "MENU". Print it centered inside a field of width 20, padded with -, using .center().

Q31PalindromeHard

Create word = "madam". Print whether it's a palindrome by comparing it to its own reverse — no loop needed.

Q32PalindromeHard

Create word = "Racecar". Print whether it's a palindrome, ignoring letter case.

Q33AnagramHard

Create word1 = "listen" and word2 = "silent". Print whether they're anagrams of each other using sorted().

Q34SlicingHard

Create text = "abcdefghij". Using one slice expression, print the substring from index 2 to 8, taking every second character.

Q35String MultiplicationHard

Build a string by repeating "ab" five times, then print only the middle 4 characters of the result using slicing.

Q36String MethodsHard

Create code = "42". Print it zero-padded to 5 digits using .zfill(). Separately, print "7" right-justified and left-justified in a field of width 5 using .rjust() and .ljust().

Q37String MethodsHard

Create text = "Hello World". Print the result of .swapcase(), then print whether text.istitle() is True.

Q38String MethodsHard

Create text = "xxHelloxx". Print it with the "x" characters stripped from both ends using .strip("x").

Q39String MethodsHard

Create record = "name:Riya". Use .partition(":") to split it into the part before, the separator, and the part after, and print all three.

Q40String MethodsHard

Create noisy = " Hello-World ". In a single chained expression, strip whitespace, replace "-" with " ", and lowercase the result, then print it.

Q41Mini-ProjectMini-Project

Build a "Username Generator". Ask for a first name, last name, and birth year, then generate a username like riya.sharma99 (lowercase, dot-separated, last 2 digits of the year).

Q42Mini-ProjectMini-Project

Build a "Text Cleaner". Ask for a messy sentence and print a cleaned version: stripped of outer whitespace, extra hyphens replaced with spaces, and title-cased.

Q43Mini-ProjectMini-Project

Build a combined "Palindrome & Anagram Checker". Ask for two words and print whether the first word is a palindrome, and whether the two words are anagrams of each other.

Q44Mini-ProjectMini-Project

Build an "Email Masker". Ask for an email address and print it with everything before the @ masked except the first character (e.g. s******@jrcodex.com).

Q45Mini-ProjectMini-Project

Build an "Initials & Signature Generator". Ask for a full name and job title, then print a formatted signature block using initials, .title(), and aligned formatting.

Q46InterviewInterview

Strings in Python are immutable. Demonstrate this: show that word[0] = "J" raises a TypeError, then show the correct way to produce a modified version by building a new string with slicing and concatenation.

Q47InterviewInterview

Given sentence = "the quick brown fox", print the number of characters in it excluding spaces, using .replace() and len().

Q48InterviewInterview

Given phrase = "A man a plan a canal Panama", print whether it's a palindrome once you ignore spaces and letter case — the real-world version of a palindrome check.

Q49InterviewInterview

Given text = "the cat sat on the mat with the hat", print the total word count using .split() and len(), and how many times the word "the" appears using .split().count().

Q50CapstoneInterview

Build a "String Toolkit Report". Ask the user for a sentence and print, all clearly labeled: its length, uppercase version, reversed version, total word count, first and last word, whether it's a palindrome ignoring spaces/case, and its vowel count.

Still stuck on something?

Book a free 1-on-1 session and we'll work through it together.

Book a Free Session