Loading...

Python String Methods

The story

Imagine a school system that prints ID cards for students. The system receives information like the student’s name, department, and ID number. Sometimes the input might be in lowercase, sometimes in uppercase, or even messy with extra spaces. To make the card look professional, the program must clean and format this information before printing.

That’s where Python’s string methods come in handy.

Here’s an example of a student card:

Student card

You’ve been hired to design this ID card generator. To do it properly, you’ll need to understand how to use Python string methods.

The main idea

Python strings come with built-in methods that allow you to transform, count, or format text quickly.

Some useful methods are:

  1. .capitalize() → Capitalizes the first letter of the string.
python
2 lines
|
14/ 500 tokens
1
2
name = "ali"
print(name.capitalize())   # Output: Ali
Code Tools

2. .count(substring) → Counts how many times a word or letter appears.

python
2 lines
|
21/ 500 tokens
1
2
text = "python is fun, python is powerful"
print(text.count("python"))   # Output: 2
Code Tools

3. .format() → Inserts variables into strings.

python
2 lines
|
24/ 500 tokens
1
2
msg = "Student: {}, ID: {}".format("Sara", 101)
print(msg)   # Output: Student: Sara, ID: 101
Code Tools

4. .title() → Capitalizes the first letter of every word.

python
2 lines
|
19/ 500 tokens
1
2
dept = "computer science"
print(dept.title())   # Output: Computer Science
Code Tools

5. .upper() / .lower() → Converts text to uppercase or lowercase.

python
2 lines
|
21/ 500 tokens
1
2
print("hello".upper())   # Output: HELLO
print("WORLD".lower())   # Output: world
Code Tools
If the system receives " sara " instead of "Sara", the card will look unprofessional unless we use .strip() to clean spaces and .capitalize() to fix the name. String methods help us standardize input and present it neatly.

Quick recap

  • .capitalize() → first letter uppercase
  • .count() → count occurrences
  • .format() → insert variables into strings
  • .title() → capitalize each word
  • .upper() / .lower() → change text case

Try it in code

Let’s create a simple student card:

python
10 lines
|
67/ 500 tokens
1
2
3
4
5
6
7
8
9
10
student_name = "sara"
department = "computer science"
student_id = 101

card = "=== STUDENT ID CARD ===\n"
card += "Name: {}\n".format(student_name.capitalize())
card += "Department: {}\n".format(department.title())
card += "ID: {}\n".format(student_id)

print(card)
Code Tools

Output:

text
4 lines
|
18/ 500 tokens
1
2
3
4
=== STUDENT ID CARD ===
Name: Sara
Department: Computer Science
ID: 101

Learn with AI

If you’re using an AI assistant, try this prompt:

Prompt:
Write a Python program that creates a student ID card using string methods like .capitalize(), .title(), .format(), and .upper(). The card should print name, department, and ID neatly formatted.

The AI will generate a ready-to-use solution.

Practice challenge

Awesome work! Now let’s test your understanding.

Task

Create an ID card for a student named ali, in the department data science, with ID number 2025.
The program should:

  • Capitalize the name.
  • Format the department with .title().
  • Insert all values using .format().
  • Show the department name in uppercase at the bottom.

Expected output:

text
6 lines
|
25/ 500 tokens
1
2
3
4
5
6
=== STUDENT ID CARD ===
Name: Ali
Department: Data Science
ID: 2025

Department Code: DATA SCIENCE

Solution

python
11 lines
|
81/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
student_name = "ali"
department = "data science"
student_id = 2025

card = "=== STUDENT ID CARD ===\n"
card += "Name: {}\n".format(student_name.capitalize())
card += "Department: {}\n".format(department.title())
card += "ID: {}\n".format(student_id)
card += "\nDepartment Code: {}".format(department.upper())

print(card)
Code Tools

Output:

text
6 lines
|
25/ 500 tokens
1
2
3
4
5
6
=== STUDENT ID CARD ===
Name: Ali
Department: Data Science
ID: 2025

Department Code: DATA SCIENCE
Perfect! You’ve now learned how to use Python’s string methods to clean, manipulate, and format text effectively.

Frequently Asked Questions

String formatting in Python refers to the method of embedding values inside a string, making the string more dynamic and readable.

%s is used for formatting strings, while %d is used to format integers.

%2f is used for formatting floating-point numbers, where the number will be printed with at least two characters for the whole number part.

The .2f is used to format a floating-point number to two decimal places. For example, %.2f will display a float rounded to two decimal places.

Still have questions?Contact our support team