String Manipulation in Python for Pharmacy Students (Drug Names, Prescriptions & Text Processing)
String manipulation is a powerful feature in Python used to handle and process text data. In pharmaceutical sciences, it is widely used for handling drug names, prescription details, patient records, and reports.
๐ท What is a String?
A string is a sequence of characters enclosed in quotes.
drug = "Paracetamol"
๐ท Accessing Characters (Indexing)
drug = "Aspirin" print(drug[0]) # A
Index starts from 0.
๐ท String Slicing
drug = "Ibuprofen" print(drug[0:4]) # Ibu
Used to extract part of a string.
๐ท Common String Methods
| Method | Example | Use |
|---|---|---|
| upper() | drug.upper() | Convert to uppercase |
| lower() | drug.lower() | Convert to lowercase |
| strip() | text.strip() | Remove spaces |
| replace() | text.replace(“a”,”o”) | Replace characters |
๐ท String Concatenation
drug = "Paracetamol" dose = "500 mg" prescription = drug + " " + dose print(prescription)
๐ท String Formatting
drug = "Ibuprofen"
dose = 400
print(f"Take {drug} {dose} mg twice daily")
๐ Prescription Processing Example
drug = " paracetamol "
clean_drug = drug.strip().capitalize()
print(f"Prescribed Drug: {clean_drug}")
This removes extra spaces and formats drug name correctly.
๐ท Searching in Strings
text = "Patient is taking Aspirin"
if "Aspirin" in text:
print("Drug found")
๐ง Memory Tricks
- upper() โ CAPS
- lower() โ small
- strip() โ remove spaces
- replace() โ change text
๐งช Practice Exercise
Create a program that:
- Takes drug name as input
- Removes extra spaces
- Displays formatted output
๐ MCQs
- Which method converts text to uppercase?
a) lower()
b) upper()
c) strip()
d) replace()
Answer: b - strip() is used to:
a) Add text
b) Remove spaces
c) Convert case
d) Replace text
Answer: b - Which operator joins strings?
a) +
b) *
c) /
d) =
Answer: a
โ FAQs
Why is string manipulation important in pharmacy?
It helps in handling drug names, prescriptions, and patient records.
Which method removes spaces?
strip() is used to remove extra spaces.
๐ฅ Download Prescription Processing Exercises
Practice with real pharma data examples.
โก Next Topic: Python Libraries & Package Installation โ
- Installing Python
- Installing IDEs like Jupyter, VS Code, PyCharm
- Variables and Data types in Python
- Operators in Python
- Input & Output
- String Manipulation (You are on this page page)
- Libraries in Python
Question Bank : Unit 1 Python Programming Basics
For more details: Basics of Python Programming for Pharmaceutical Sciences (Theory)