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:

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:
- .capitalize() → Capitalizes the first letter of the string.
1 2name = "ali" print(name.capitalize()) # Output: Ali
2. .count(substring) → Counts how many times a word or letter appears.
1 2text = "python is fun, python is powerful" print(text.count("python")) # Output: 2
3. .format() → Inserts variables into strings.
1 2msg = "Student: {}, ID: {}".format("Sara", 101) print(msg) # Output: Student: Sara, ID: 101
4. .title() → Capitalizes the first letter of every word.
1 2dept = "computer science" print(dept.title()) # Output: Computer Science
5. .upper() / .lower() → Converts text to uppercase or lowercase.
1 2print("hello".upper()) # Output: HELLO print("WORLD".lower()) # Output: world
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:
1 2 3 4 5 6 7 8 9 10student_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)
Output:
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:
1 2 3 4 5 6=== STUDENT ID CARD === Name: Ali Department: Data Science ID: 2025 Department Code: DATA SCIENCE
Solution
1 2 3 4 5 6 7 8 9 10 11student_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)
Output:
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