Lesson · 40 min · Free
Python Strings
Python Strings Strings are one of the most fundamental data types in Python, and they appear constantly in pharmaceutical and data science work. A string is a sequence of characters used to represent text — drug names, p
Python Strings
Strings are one of the most fundamental data types in Python, and they appear constantly in pharmaceutical and data science work. A string is a sequence of characters used to represent text — drug names, patient IDs, gene sequences, lab reports, or column headers in a dataset. In Python, you create a string by enclosing text in single quotes ( '...' ) or double quotes ( "..." ), and both are equivalent. Strings are immutable , meaning once created, they cannot be changed in place. Instead, every operation you perform on a string returns a new string. This is an important concept when processing large amounts of text data, such as cleaning up drug names in a pharmacy database or parsing entries from a clinical trial log. Python provides a rich set of built-in string methods — such as .upper() , .lower() , .strip() , .replace() , and .split() — that make text manipulation straightforward and efficient. Another essential feature is indexing and slicing . Because strings are sequences, each character has a position (index) starting at 0. Negative indices count from the end, and slicing with the syntax string[start:stop:step] lets you extract substrings. This is especially useful in bioinformatics contexts, for example when extracting codons from a DNA sequence stored as a string. Finally, f-strings (formatted string literals) are the modern, preferred way to embed variables and expressions inside text. They are invaluable when generating patient-facing output, formatted lab summaries, or reports that combine computed values with descriptive labels. # Example 1: Basic string operations for a drug record drug_name = " Metformin HCl " # Clean and standardize the drug name clean_name = drug_name.strip().upper() print(clean_name) # Output: METFORMIN HCL # Replace part of the string generic_name = clean_name.replace("HCL", "HYDROCHLORIDE") print(generic_name) # Output: METFORMIN HYDROCHLORIDE # Check length and membership print(len(clean_name)) # Output: 13 print("METFORMIN" in clean_name) # Output: True # Example 2: Slicing a DNA sequence and f-string formatting dna_sequence = "ATGCGTACCTTAGGC" # Extract the first three nucleotides (the start codon) start_codon = dna_sequence[0:3] print(f"Start codon: {start_codon}") # Output: Start codon: ATG # Extract the last three nucleotides (stop codon region) end_codon = dna_sequence[-3:] print(f"End codon: {end_codon}") # Output: End codon: AGC # Compute GC content as a percentage and format to 2 decimals gc_count = dna_sequence.count("G") + dna_sequence.count("C") gc_content = (gc_count / len(dna_sequence)) * 100 print(f"GC content: {gc_content:.2f}%") # Output: GC content: 46.67% Strings are immutable sequences of characters, defined with single or double quotes. String methods like .strip() , .upper() , .replace() , and .split() return new strings rather than modifying the original. Indexing starts at 0; negative indices count from the end, and slicing uses the [start:stop:step] syntax. The in operator tests whether a substring exists within a string. F-strings ( f"...{variable}..." ) provide clean, readable formatting, including decimal precision with format specifiers like :.2f . Practice exercise: Create a variable containing the string " atorvastatin calcium " . Strip the whitespace, convert it to uppercase, and replace "CALCIUM" with "CALCIUM SALT" . Then compute and print the length of the cleaned name and use an f-string to report the percentage of the string's characters that are vowels, rounded to one decimal place.
Watch the full lesson — free
This topic is part of The Complete Python Bootcamp: Basics to Pharma & Data Science, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →