Python Strings – Part 3

A colourful cartoon-style landscape illustration shows the words “python strings” in lowercase being fed into a large whimsical machine. On the other side, the text emerges transformed into “PYTHON STRINGS” in uppercase letters, symbolising string methods like .upper(). The machine has gears and pipes, giving a fun and playful feel.

In Part 1, you learnt the basics of strings: quotes, printing, joining, length, and indexes.
In Part 2, you discovered escaping characters, multi-line strings, and slicing.

Now, in Part 3, we’ll explore string methods — special built-in tools that come with Python. These methods let you quickly change, tidy, or check strings without writing lots of extra code yourself. Think of them like “apps” that come pre-installed for working with text.

Changing the case of text

Strings often need to be written in a certain style — all capitals, all lowercase, or with proper names capitalised. Python makes this easy.

These methods don’t change the original string — they create a new version.

Try this

  1. Create a variable with your name in lowercase and print it in uppercase.
  2. Try .title() with "the lord of the rings". What happens?

Removing extra spaces

Sometimes when text comes from user input or files, it contains extra spaces at the beginning or end. This can cause errors or messy output. Python has methods to clean this up:

This is especially useful when you’re asking someone to type something with input() — they may accidentally press spacebar before or after.

Try this

  1. Make word = " Python ".
  2. Print it normally, then with .strip(). Can you see the difference?

Replacing parts of a string

If you want to swap one piece of text for another, use .replace(old, new). This is useful for editing sentences or fixing mistakes.

You can even chain replacements to make several changes at once.

Try this

  1. Make phrase = "I love chips".
  2. Replace "chips" with "pizza".
  3. Replace "love" with "adore".

Splitting and joining

Strings often need to be broken into pieces (like splitting a sentence into words), or joined back together.

  • .split(separator) breaks a string into a list.
  • "separator".join(list) joins a list back into a single string.

This is useful when handling CSV files (comma-separated values) or when formatting data neatly.

Try this

  1. Split "one two three" into a list of words.
  2. Join ["tea", "coffee", "juice"] into one string separated by ;.

Checking what’s inside a string

Python can test if a string contains certain types of characters. This is helpful when validating input, such as checking if someone typed a number or a word.

You can also check how a string begins or ends:

Try this

  1. Make code = "PY123". Use .isalnum() to check it.
  2. Make filename = "photo.jpg". Check if it ends with .jpg.

Putting it all together

Here’s a short program that uses several methods together:

This shows how you can combine methods step by step to clean up and improve text.

Try this

Write a program that:

  1. Asks the user for their favourite food (use input()).
  2. Removes spaces from the answer with .strip().
  3. Prints it back in uppercase.

Summary

  • .upper(), .lower(), .title(), .capitalize() change the case of text.
  • .strip(), .lstrip(), .rstrip() remove spaces.
  • .replace() swaps text.
  • .split() breaks a string into a list, .join() joins a list into a string.
  • .isdigit(), .isalpha(), .isalnum(), .isspace() check the content of strings.
  • .startswith() and .endswith() check beginnings and endings.

✅ With these methods, you can now clean up, transform, and check text with just a few simple commands.

Main Topic

Python Fundamentals

A colourful cartoon-style landscape illustration shows a person sitting at a computer terminal, looking overwhelmed by the amount of information on the screen. Papers and symbols float around them, representing different programming concepts. The word “Fundamentals” is clearly written above the scene, highlighting the focus on beginner Python basics.

Beginner’s guide to Python fundamentals: strings, numbers, booleans, syntax, and text manipulation explained simply and clearly.

Other Tutorials in this Topic

A playful cartoon-style landscape illustration shows a large elephant on the left and a small cat on the right. Between them, a bold greater than ( > ) symbol points toward the cat, representing a Python comparison operator in a fun way.

Python Operators

Beginner’s guide to Python operators: arithmetic, comparison, logical, assignment, membership, and identity explained with examples.

A colourful cartoon-style landscape illustration shows a smiling robot holding up two placards: one clearly labelled True and the other False. The background is bright and cheerful, symbolising the concept of Boolean values in Python programming.

Python Booleans

Learn Python Booleans: True or False values, comparisons, logic operators, and decision-making with simple examples.

ChatGPT said: A colourful cartoon-style landscape illustration shows two large labelled containers: one marked Integers filled with whole numbers like 7, 0, and -3, and another marked Floats filled with decimal numbers like 3.14, -0.5, and 2.0. A cheerful character is sorting the numbers into the correct containers, visually explaining the difference between integers and floating-point numbers in Python.

Python Numbers

Learn Python numbers: integers and floats, arithmetic operations, conversions, rounding, precision issues, and practical exercises for beginners…

A colourful cartoon-style landscape illustration shows the text “python ” written across the scene. Above the gap, a labelled box with the word “strings” is tilted, pouring its contents into the space, visually representing a variable being used to fill in a placeholder.

Python Strings – Part 4

Discover Python string formatting: use f-strings, .format(), and number formatting to neatly combine variables and text into…

A digital landscape illustration shows a beige fabric banner with the words “Python Strings” written across it. The material is ripped down the centre, with large visible stitches crudely sewing the two halves back together, symbolising fixing or escaping characters in strings. The background is simple and muted, drawing attention to the torn fabric.

Python Strings – Part 2

Explore Python strings further: escape characters, multi-line strings, and slicing to extract parts of text, with simple…

A colourful cartoon-style landscape illustration shows festive bunting strung across the top, with triangular flags spelling out “Python Strings” in bright, playful letters. The background is light and cheerful, giving a fun and welcoming atmosphere.

Python Strings – Part 1

Learn Python strings: text in quotes, printing, joining with +, counting length, and accessing characters by position…

A colourful cartoon-style illustration shows a confused student with orange hair shrugging in front of a computer screen. The screen displays a Python function with a missing indentation error:

Python Syntax

Learn Python syntax basics with clear examples and beginner-friendly practice exercises.

A cartoon-style illustration shows a person’s hands typing on a laptop running Visual Studio Code in dark mode. The screen displays a Python file with the code print("Hello, world!"), alongside a large, colourful Python logo. The background is bright blue, creating a vibrant and engaging learning atmosphere.

Start Coding Python with VS Code

Beginner’s guide to installing Python, setting up VS Code, and getting ready for coding.

Python Variables

Python Variables

Learn Python variables with simple explanations, real-world analogies, practice tasks to build confidence using data in programs.