Strings — Practice Questions
50 questions. Try each one yourself before checking the answer.
Create word = "Python". Print its first character using indexing.
Using word = "Python", print its last character using negative indexing.
Using word = "Python", print the character at index 2.
Create a string with your favorite programming language's name and print its length using len().
Using word = "Python", print the first three characters using slicing.
Using word = "Python", print the last three characters using slicing.
Using word = "Python", print the string with the first and last characters removed.
Create text = "Hello World". Print it in all uppercase using .upper() and all lowercase using .lower().
Create text = " messy input ". Print it with leading and trailing whitespace removed using .strip().
Create title = "the great gatsby". Print it with each word capitalized using .title().
Using word = "Python", print the string reversed using slicing with a step of -1.
Using word = "Python", print every second character using a step of 2.
Create sentence = "I like cats". Replace "cats" with "dogs" using .replace() and print the result.
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.
Create text = "mississippi". Print how many times "s" and "i" each appear, using .count().
Create sentence = "learning python is fun". Split it into a list of words using .split() and print the result.
Create a list words = ["learning", "python", "is", "fun"]. Join them back into a single sentence separated by spaces using .join().
Create filename = "report.pdf". Print whether it .startswith("report") and whether it .endswith(".pdf").
For each of "abc", "123", and "abc123", print the result of .isalpha(), .isdigit(), and .isalnum().
Create text = "hello WORLD". Print the result of .capitalize() and .title() side by side and explain the difference in a comment.
Ask the user for a username, then print a "cleaned" version: whitespace stripped and lowercased.
Ask for a first name and last name, then print a nicely formatted full name using .title() on each.
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.
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()).
Create word = "encyclopedia". Count the total number of vowels (a, e, i, o, u) it contains by summing individual .count() calls.
Create email = "student@jrcodex.com". Extract and print just the domain (jrcodex.com) using .split("@") and indexing.
Create filename = "assignment.final.docx". Extract and print just the file extension (docx) using .split(".") and indexing.
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.
Create sentence = "please review the attached invoice". Print whether the word "invoice" appears in it, using in.
Create title = "MENU". Print it centered inside a field of width 20, padded with -, using .center().
Create word = "madam". Print whether it's a palindrome by comparing it to its own reverse — no loop needed.
Create word = "Racecar". Print whether it's a palindrome, ignoring letter case.
Create word1 = "listen" and word2 = "silent". Print whether they're anagrams of each other using sorted().
Create text = "abcdefghij". Using one slice expression, print the substring from index 2 to 8, taking every second character.
Build a string by repeating "ab" five times, then print only the middle 4 characters of the result using slicing.
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().
Create text = "Hello World". Print the result of .swapcase(), then print whether text.istitle() is True.
Create text = "xxHelloxx". Print it with the "x" characters stripped from both ends using .strip("x").
Create record = "name:Riya". Use .partition(":") to split it into the part before, the separator, and the part after, and print all three.
Create noisy = " Hello-World ". In a single chained expression, strip whitespace, replace "-" with " ", and lowercase the result, then print it.
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).
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.
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.
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).
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.
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.
Given sentence = "the quick brown fox", print the number of characters in it excluding spaces, using .replace() and len().
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.
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().
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