April 24, 2026

String Manipulation in Python (with Drug Names, Prescriptions & Text Processing)

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"
๐Ÿ’ก Key Insight: Strings are used to store textual data such as drug names and patient information.

๐Ÿ”ท 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

MethodExampleUse
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

  1. Which method converts text to uppercase?
    a) lower()
    b) upper()
    c) strip()
    d) replace()
    Answer: b

  2. strip() is used to:
    a) Add text
    b) Remove spaces
    c) Convert case
    d) Replace text
    Answer: b

  3. 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 โ†’

  1. Installing Python
  2. Installing IDEs like Jupyter, VS Code, PyCharm
  3. Variables and Data types in Python
  4. Operators in Python
  5. Input & Output
  6. String Manipulation (You are on this page page)
  7. Libraries in Python

Question Bank : Unit 1 Python Programming Basics

For more details: Basics of Python Programming for Pharmaceutical Sciences (Theory)