I1 · Python Programming for AI

String Methods and Formatting

String methods are built-in functions attached to strings, called with a dot, such as text.upper(). They return a new, changed string. f-strings, written f"…{value}…", insert values into text and can format numbers, for example to two decimal places.

10 min Intermediate Jump to the activity

What you need: The earlier topics of this module.

Definition

How do you turn " RIYA sharma " into "Riya Sharma"?

String methods are built-in functions attached to strings, called with a dot, such as text.upper(). They return a new, changed string. f-strings, written f"…{value}…", insert values into text and can format numbers, for example to two decimal places.

In simple words

strip() trims spaces from the ends, lower() and upper() change case, title() capitalises each word, replace() swaps one piece of text for another, and split() breaks a sentence into a list of words. join() does the reverse. f"{name} scored {marks}" builds a sentence from variables.

Why it matters

Real text is messy: extra spaces, mixed case, typos. Cleaning it is the first step before any analysis or AI model can use it, and clear formatted output makes results easy to read.

How it works

MethodExampleResult
strip()" hi ".strip()"hi"
lower() / upper()"Ai".upper()"AI"
title()"riya sharma".title()"Riya Sharma"
replace()"cat".replace("c", "b")"bat"
split()"a b c".split()["a", "b", "c"]
join()"-".join(["a", "b"])"a-b"
f-stringf"{2/3:.2f}""0.67"

In code

Cleaning a messy name, counting words, and formatting a report line.

clean_input.py
raw = "  RIYA sharma  "
clean = raw.strip().title()
print("Clean name:", clean)

sentence = "AI is fun and AI is useful"
words = sentence.split()
print("Words:", words)
print("Number of words:", len(words))
print("Times 'AI' appears:", sentence.count("AI"))
print("Joined with dashes:", "-".join(words[:3]))

marks, total = 47, 60
print(f"{clean} scored {marks}/{total} = {marks / total * 100:.1f}%")

Output

Clean name: Riya Sharma
Words: ['AI', 'is', 'fun', 'and', 'AI', 'is', 'useful']
Number of words: 7
Times 'AI' appears: 2
Joined with dashes: AI-is-fun
Riya Sharma scored 47/60 = 78.3%

Try it yourself

Which method?

Match each job to the method that does it.

  • Remove spaces from both ends
  • Make every letter capital
  • Break a sentence into words
  • Swap one word for another
  • Glue a list of words with a separator

Common mistakes

  • Expecting a method to change the original string. Store the result: name = name.strip().
  • Forgetting the f before an f-string, so {name} is printed literally.
  • Calling join on the list instead of the separator: it's "-".join(words).

Check yourself

Question 1

1. What is " Hi ".strip()?

Question 2

2. What does "a,b,c".split(",") return?

Question 3

3. f"{3.14159:.2f}" gives…

Summary

  • Methods such as strip, lower, title, replace and split clean text.
  • They return new strings; store the result.
  • f-strings insert and format values neatly.

Finished reading?

Sources: W3Schools: Python String Methods