String methods and f-strings
A method is a function that belongs to a value. You call it with a dot after the value: name.upper(). Strings come with dozens of methods. This lesson covers the ones you will use every week, then f-strings, the modern way to build text from variables.
Remember from the last lesson that strings cannot be changed in place. Every method here returns a new string. The original stays as it was unless you store the result.
Changing case
name = "asha VERMA"
print(name.upper())
print(name.lower())
print(name.title())
print(name.capitalize())
print(name)Output
ASHA VERMA
asha verma
Asha Verma
Asha verma
asha VERMAThe last line shows the original is untouched. lower is very useful when comparing what a user typed: turn both sides to small letters first, and "YES", "Yes" and "yes" all match.
Removing spaces from the ends
Text typed by people often has stray spaces. strip removes spaces, tabs and new lines from both ends. lstrip and rstrip do one side only.
raw = " hello world "
print("[" + raw.strip() + "]")
print("[" + raw.lstrip() + "]")
print("[" + raw.rstrip() + "]")Output
[hello world]
[hello world ]
[ hello world]The square brackets are only there so that you can see where the string ends.
Replace, find and count
line = "I like tea. Tea is hot."
print(line.replace("tea", "coffee"))
print(line.find("like"))
print(line.find("milk"))
print(line.count("e"))Output
I like coffee. Tea is hot.
2
-1
3replace swaps every match and is exact about capital letters, so the second "Tea" stayed. find gives the index where the match starts, or -1 when there is no match. count says how many times something appears.
Questions about a string
These methods answer with True or False.
print("12345".isdigit())
print("abc12".isdigit())
print("hello".isalpha())
print("report.pdf".endswith(".pdf"))
print("Mr Sharma".startswith("Mr"))Output
True
False
True
True
TrueSplit and join
split cuts a string into a list of pieces. With nothing in the brackets it cuts at spaces. Give it a separator to cut somewhere else.
sentence = "the quick brown fox"
print(sentence.split())
row = "Asha,14,Ludhiana"
print(row.split(","))Output
['the', 'quick', 'brown', 'fox']
['Asha', '14', 'Ludhiana']join does the reverse. The string before the dot is the glue that goes between the pieces.
words = ["2026", "11", "12"]
print("-".join(words))
print(" ".join(["Good", "morning"]))Output
2026-11-12
Good morningLists get their own lesson soon. For now, read the square brackets as "a row of items".
f-strings
An f-string is a string with the letter f before the opening quotation mark. Inside it, anything in curly brackets is replaced by its value. It is the cleanest way to mix text and variables.
name = "Ravi"
marks = 87
print(f"{name} scored {marks} marks.")
print(f"Next year {name} will be in class {8 + 1}.")Output
Ravi scored 87 marks.
Next year Ravi will be in class 9.You can put a sum, or a method call, inside the curly brackets.
Formatting numbers
After the value, a colon starts a format code.
price = 1234.5
share = 0.256
print(f"Price: {price:.2f}")
print(f"With commas: {1234567:,}")
print(f"Share: {share:.1%}")
print(f"Roll no: {7:03d}")Output
Price: 1234.50
With commas: 1,234,567
Share: 25.6%
Roll no: 007.2fmeans two digits after the decimal point.,adds commas every three digits. Note that this is the international style, not the Indian lakh and crore grouping..1%multiplies by 100 and adds a percent sign.03dpads a whole number with zeros to three digits.
Lining up columns
A number after the colon sets a width. < pushes text to the left and > to the right.
print(f"{'Item':<10}{'Price':>6}")
print(f"{'Tea':<10}{10:>6}")
print(f"{'Samosa':<10}{15:>6}")Output
Item Price
Tea 10
Samosa 15Try it yourself
Exercise 1. A user typed " RAVI kumar ". Clean it so it prints as Ravi Kumar with no spaces at the ends.
Show one answer
raw = " RAVI kumar "
print(raw.strip().title())Output
Ravi KumarExercise 2. Count how many words are in the sentence "practice makes a person perfect".
Show one answer
sentence = "practice makes a person perfect"
print(len(sentence.split()))Output
5Exercise 3. Given item = "Rice", kg = 5 and rate = 62.5, print one line like Rice: 5 kg costs Rs 312.50 using an f-string.
Show one answer
item = "Rice"
kg = 5
rate = 62.5
print(f"{item}: {kg} kg costs Rs {kg * rate:.2f}")Output
Rice: 5 kg costs Rs 312.50Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.
This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.