1. What is a string?
A string is a sequence of characters used to store text.
medicine_name = “Paracetamol”
dosage_form = “Tablet”
batch_number = “TAB-2026-015”
storage_instruction = “Store below 25°C”
The quotation marks tell Python that the value is text.
| Value | Data type | Meaning |
| “500” | str | Text containing digits |
| 500 | int | Whole number |
| “25.5” | str | Decimal written as text |
| 25.5 | float | Decimal number |
2. Why do we use string manipulation?
Pharmaceutical records contain text that may be entered inconsistently:
” paraCETamol “
“PARACETAMOL”
“paracetamol”
“Paracetamol”
Although these appear similar to a person, Python treats them as different strings.
String manipulation helps us:
- Remove unnecessary spaces.
- Standardise capitalisation.
- Search medicine descriptions.
- Separate batch-code sections.
- Validate whether required fields are present.
- Prepare readable labels.
- Create inventory summaries.
- Standardise imported text data.
3. Important string operations
| Operation | Syntax | Purpose |
| Create string | name = “Paracetamol” | Store text |
| Length | len(name) | Count characters |
| Indexing | name[0] | Retrieve one character |
| Slicing | name[0:4] | Retrieve several characters |
| Concatenation | text1 + text2 | Join strings |
| Repetition | “-” * 20 | Repeat text |
| Lowercase | .lower() | Convert to lowercase |
| Uppercase | .upper() | Convert to uppercase |
| Title case | .title() | Capitalise words |
| Remove spaces | .strip() | Clean both ends |
| Replace | .replace() | Replace selected text |
| Search | .find() | Find first position |
| Count | .count() | Count occurrences |
| Membership | in, not in | Check text presence |
| Prefix check | .startswith() | Check beginning |
| Suffix check | .endswith() | Check ending |
| Split | .split() | Divide text into parts |
| Join | .join() | Combine text parts |
| Alphabet check | .isalpha() | Check for letters |
| Digit check | .isdigit() | Check for digits |
| Alphanumeric check | .isalnum() | Check letters and digits |
| Formatted text | f-string | Insert variables into text |
4. Indexing and slicing
Python starts counting characters from zero.
For the word PHARMA:
| Character | P | H | A | R | M | A |
| Positive index | 0 | 1 | 2 | 3 | 4 | 5 |
| Negative index | -6 | -5 | -4 | -3 | -2 | -1 |
word = “PHARMA”
print(word[0]) # P
print(word[-1]) # A
print(word[0:3]) # PHA
print(word[::-1]) # AMRAHP
The stop position in a slice is excluded. Therefore, [0:3] returns positions 0, 1 and 2.
Pharmacy application
batch_code = “TAB-2026-015”
product_prefix = batch_code[0:3]
year_section = batch_code[4:8]
sequence_number = batch_code[9:12]
print(product_prefix)
print(year_section)
print(sequence_number)
Output:
TAB
2026
015
This extracts visible sections but does not confirm that the batch exists.
5. Cleaning medicine names
raw_medicine_name = ” paraCETamol “
clean_medicine_name = raw_medicine_name.strip().title()
print(“Original:”, repr(raw_medicine_name))
print(“Cleaned:”, clean_medicine_name)
Output:
Original: ‘ paraCETamol ‘
Cleaned: Paracetamol
Interpretation
- .strip() removes spaces from both ends.
- .title() standardises capitalisation.
- repr() makes hidden external spaces visible.
The result is more consistent, but Python has not verified its spelling or identity against approved medicine master data.
6. Searching label text
label_text = “Paracetamol 500 mg Tablet”
print(“Tablet” in label_text)
print(“Syrup” not in label_text)
print(label_text.find(“500 mg”))
print(label_text.count(“Paracetamol”))
Output:
True
True
12
1
Interpretation
- “Tablet” in label_text checks whether the exact text is present.
- “Syrup” not in label_text confirms that the word is absent.
- .find() returns the starting position.
- .count() reports the number of exact occurrences.
These operations are case-sensitive.
7. Splitting pharmaceutical records
medicine_record = “Paracetamol|500 mg|Tablet|TAB-015”
record_parts = medicine_record.split(“|”)
print(“Medicine:”, record_parts[0])
print(“Strength:”, record_parts[1])
print(“Dosage form:”, record_parts[2])
print(“Batch:”, record_parts[3])
Output:
Medicine: Paracetamol
Strength: 500 mg
Dosage form: Tablet
Batch: TAB-015
.split(“|”) separates the text wherever the vertical bar appears.
To join the parts again:
readable_record = ” – “.join(record_parts)
print(readable_record)
8. String validation methods
batch_code = “TAB-2026-015”
parts = batch_code.split(“-“)
prefix = parts[0]
year_text = parts[1]
sequence_text = parts[2]
print(“Prefix contains letters:”, prefix.isalpha())
print(“Year contains digits:”, year_text.isdigit())
print(“Sequence contains digits:”, sequence_text.isdigit())
Output:
Prefix contains letters: True
Year contains digits: True
Sequence contains digits: True
This validates only the visible pattern:
letters-digits-digits
It does not authenticate the batch through a manufacturing database.
9. Formatted output using f-strings
medicine_name = “Cetirizine”
strength_mg = 10
dosage_form = “Tablet”
batch_number = “CTZ-026”
label = (
f”{medicine_name} {strength_mg} mg “
f”{dosage_form} | Batch: {batch_number}”
)
print(label)
Output:
Cetirizine 10 mg Tablet | Batch: CTZ-026
F-strings are usually easier to understand than repeatedly using +.
10. String immutability
Strings are immutable. This means an existing string cannot be changed character by character.
raw_name = “paracetamol”
clean_name = raw_name.title()
print(raw_name)
print(clean_name)
Output:
paracetamol
Paracetamol
.title() created and returned a new string. The original string remained unchanged.
11. Pharmaceutical-industry applications
| Area | Application |
| Inventory | Standardising medicine names |
| Manufacturing | Separating visible batch-code sections |
| Quality control | Formatting sample summaries |
| Packaging | Preparing prototype label text |
| Warehousing | Checking location-code prefixes |
| Documentation | Detecting blank mandatory fields |
| Data cleaning | Replacing inconsistent delimiters |
| Reporting | Creating readable output using f-strings |
| Pharmacovigilance preparation | Standardising text categories before expert review |
String operations improve consistency, searchability, and presentation. They do not establish medicine quality, safety, identity, efficacy, or regulatory compliance.
Dr. Arpana Chaturvedi
HOD -IT , Associate Professor (Department of IT and Data ANalytics/AI ML)