HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Programming
  2. /Python Basics
  3. /Strings

Strings

Python Programming · Middle Stage (6–8) · 1 course, 13 worked problems, 57 practice questions

A string is a piece of text written between quotes, such as name = "Asha". Each character has an index that starts at 0, so name[0] is "A". A slice such as name[1:3] gives part of the string. Strings can be joined with + and repeated with *, and len() gives their length. Methods such as .upper(), .lower(), .strip() and .replace() return changed copies, because a string itself cannot be changed. An f-string puts values into text, and a for loop can visit each character in turn.

With practice you will be able to

  • Get characters and slices from a string by index
  • Change text with methods such as .upper() and .replace()
  • Loop over a string to count letters or vowels

On the syllabus of

  • CBSE / State Class 12
  • Python Certification
  • AP
  • IB
  • A-Level
  • IGCSE

Lessons

  • Words and Text: StringsHow do you write a word backwards? Why can you not change one letter of a string?Middle Stage (6–8)

Practice questions

Choose an answer, then check it. Each one shows a full explanation.

A program takes two characters and a slice from a word. s = "certify" print(s[2], s[-2], s[1:4]) What does the program print?

A student wants to change the word "data" so that it starts with a capital D. The program starts with s = "data", and then one of four lines is run. s = "data" Which line runs with no error?

A loop cuts a string into pieces of two characters and builds a new string from the pieces. s = "abcdef" t = "" for i in range(0, len(s), 2): t = s[i:i + 2] + t print(t) What does the program print?

Worked problems

Each one has a hint, the key idea and a full worked solution.

  • The variable word = "elephant" has eight letters…Easy
  • A student wants to change the name "Rahul" to "Raju" and writes…Medium
  • For the text "the cat sat on the mat", write the program lines that show: how many times at occurs…Medium
  • For text = "Python Programming", find its length, its capital letters version, its small letters…Easy
  • For word = "computer", what are word[0], word[-1], word[2:5] and word[::-1]?Medium

Python labs

  • Pick letters out
  • Cut with slices
  • Clean the name
  • Build a sentence
  • Text detective
  • Project: the name tag

Related topics

  • Python Basics
  • Getting Started with Python
  • Variables and Data Types
  • Operators and Expressions
  • Input and Output
  • Conditionals: if, elif, else
  • Loops
  • Lists
  • Functions
  • Mini Projects

Five claims about the word loop use the in operator, slices that start inside or past the end, and the ordering of text. Pick the claims that hold. More than one may be right.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

A function builds the initials of a name, which has two spaces after its first word. Then the program splits a text on one space. def initials(name): out = "" for word in name.split(): out += word[0].upper() return out print(initials("ada king lovelace"), len("ada king".split(" "))) What happens when the program runs?

A method of a class calls a string method on an attribute. class Label: def __init__(self, text): self.text = text def hide(self): self.text.replace("o", "0") return self.text + "!" a = Label("door") print(a.hide(), a.text) What does the program print?

A class keeps a code with its spaces removed, and a method picks out its digits. class Code: def __init__(self, raw): self.raw = raw.strip() def digits(self): return "".join(ch for ch in self.raw if ch.isdigit()) c = Code(" A-12-b7 ") print(c.digits(), int(c.digits()) + 1, c.raw.split("-")) What does the program print?

A program finds the dots in a file name. path = "data.backup.tar.gz" first = path.find(".") last = path.rfind(".") print(first, last, path[last + 1:], path[:first].upper()) What does the program print?

A class has a __str__ method, and a program joins its objects into one line. class Tag: def __init__(self, name): self.name = name def __str__(self): return "#" + self.name tags = [Tag("py"), Tag("oop")] print(" ".join(str(t) for t in tags)) print(" ".join(tags)) What happens when the program runs?

This function should tell whether a text contains a tag. The program should print True False, but it prints False True. def has_tag(text, tag): if text.find(tag): return True return False print(has_tag("#news today", "#news"), has_tag("plain", "#")) Which one change makes it print True False?

All 57 practice questions on Strings →

Show the first letter and the last letter of the word python.
Easy
  • A student writes word[0] = "J" to change Python into Jython…Medium
  • What do print("ha" * 3), print("Py" + "thon"), print("ab" * 2 + "c") and print("-" * 20) show?Easy
  • name = "Asha" and age = 12. Use an f-string to show Asha is 12 years old…Easy
  • Three notebooks cost 249.5 rupees each. Show the total with exactly two decimal places, in the form…Medium
  • A word that reads the same forwards and backwards is a palindrome…Medium
  • For word = "banana", how many times do a, an and n appear?Easy
  • All 13 worked problems on Strings →