STR Coding Club logo STR Coding ClubLearn to code, together
Python and Practice

Python String Practice Exercises for Absolute Beginners

Most beginner programs spend their time handling text: names typed by users, lines read from files, messages printed back to the screen.

Two students at a classroom table looking at a laptop screen with short lines of Python code while one points at the screen

Most beginner programs spend their time handling text: names typed by users, lines read from files, messages printed back to the screen. Yet new coders often jump past strings to loops and functions, then get stuck when a program breaks because of a stray space or a capital letter. Python string practice fixes that gap quickly, and the six exercises below are the ones I use in our club sessions with people in their first month of coding.

Each exercise has a goal, the string tools you need, a hint, and a way to check your answer. Try each one for 15 minutes before reading the hint.

The string tools you need before you start

You do not need to memorise dozens of methods. Six tools cover almost every beginner task:

  • len(text) gives the number of characters, including spaces.
  • Indexing such as text[0] gives the first character, and text[-1] gives the last one.
  • Slicing such as text[0:5] gives the first five characters.
  • text.lower() and text.upper() return a new string in one case.
  • text.strip() removes spaces and line breaks from both ends.
  • text.split() breaks a string into a list of words, and " ".join(words) puts them back together.

One rule trips up almost everyone: strings cannot be changed in place. Calling name.upper() does not change name. It returns a new string, so you must store it, as in name = name.upper(). If an exercise below seems to do nothing, check this first.

Six Python string practice exercises

1. Initials from a full name

Ask the user for their full name and print their initials in capitals. "ada lovelace" should print "AL".

Hint: split the name into words, take index 0 of each word, then join and uppercase the result. Check: test with a name that has three parts, such as "mary jane watson", and with extra spaces at the start.

2. Count the vowels

Write a program that counts the vowels in a sentence. "Programming is fun" has 5.

Hint: loop over each character, lowercase it, and check whether it is in the string "aeiou". Keep a counter that starts at 0. Check: try a sentence in all capitals. If your count drops to 0, you forgot to lowercase.

3. Palindrome checker

Decide whether a word reads the same backwards. "level" and "Racecar" should both return True.

Hint: the slice text[::-1] reverses a string. Compare the lowercased word with its reversed version. Check: once that works, extend it so "Never odd or even" also passes by removing spaces before comparing.

4. Clean up messy input

Users type badly. Take an input like " hELLo WoRLD " and print "Hello world".

Hint: strip first, then lowercase, then use text.capitalize(), which uppercases only the first character. Check: print the result between two pipe characters, like |Hello world|, so you can see whether any spaces survived.

5. Word length report

Given a sentence, print each word with its length, then print the longest word. For "the quick brown fox", the longest is "quick" or "brown", both 5 letters.

Hint: split into a list, loop over it, and track the longest word seen so far in a variable. Check: decide what should happen when two words tie, and make your program do that on purpose rather than by accident.

6. Simple username generator

Combine a first name, the first three letters of a surname and a birth year to make a username. "Sam", "Okafor" and 2009 become "samoka2009".

Hint: the year is a number, so convert it with str(year) before joining. An f-string such as f"{first}{last[:3]}{year}" is the neatest way. Check: test with a surname shorter than three letters, like "Li". Slicing does not crash on short strings, which surprises many beginners.

Common mistakes during Python string practice

After running these exercises with roughly 60 learners, the same errors appear again and again. If your program misbehaves, check this list before asking for help:

  1. Forgetting to store the result. text.strip() on its own line does nothing useful.
  2. Mixing strings and numbers. "Age: " + 12 raises a TypeError. Use str(12) or an f-string.
  3. Off by one slicing. text[0:3] gives three characters, positions 0, 1 and 2. The end number is not included.
  4. Comparing different cases. "Yes" and "yes" are not equal. Lowercase user input before comparing it.
  5. Index out of range. text[10] on a five character string crashes, even though text[0:10] does not.

For a wider list of errors that are not specific to strings, see common Python mistakes new coders repeat.

Why you should not memorise every string method

A lot of beginner courses hand out a sheet of 40 string methods and suggest learning them all. I think that wastes time early on. Python has methods like casefold, partition, zfill and expandtabs that you may not need for a year. Drilling them crowds out the real skill, which is breaking a text problem into small steps.

Look at exercise 4 again. Nobody needs a special method for "clean messy input". You need to see that the problem is three smaller problems: remove spaces, fix case, capitalise the start. Once you can split problems like that, you can look up any method in ten seconds. The reverse does not work. Knowing 40 method names does not tell you which three to chain together.

So learn the six tools above well, solve problems with them, and add new methods only when a real exercise needs one. That is also how our club approaches practising Python with small daily exercises.

How to keep going after these six

Once all six exercises work, change one rule in each and solve it again. Make the initials program add full stops, as in "A.L.". Make the vowel counter report each vowel separately. Make the palindrome checker ignore punctuation too. Changing the rules forces you to reread your own code, which is where most of the learning happens.

If you get stuck on a variation for more than 20 minutes, write out the steps in plain words before touching the code again. Our guide on getting unstuck on a practice problem without peeking covers that method in detail.

Frequently asked questions

What should I practice first with Python strings?

Start with indexing, slicing, len and the case methods, because almost every other string task builds on them. After that, practise split and join, which turn sentences into lists of words and back again. Those few tools cover most beginner programs.

Why does my Python string method not change the string?

Strings in Python are immutable, which means they cannot be changed once created. Methods like upper, strip and replace return a new string instead. Assign the result back to a variable, for example text = text.strip(), and the change will stick.

How many string exercises should a beginner do?

Quality matters more than volume. Six to ten exercises done properly, including the edge cases and one variation each, will teach you more than 50 rushed ones. Aim for about 30 minutes a day over a week and you will be comfortable with the basics.

SD
Selene Dragomir

Selene coaches students for programming contests. She writes about practice problems, debugging and the habits that help beginners get unstuck on their own.

More posts by Selene

More in Python and Practice

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto