Introduction to Python Strings

Sep 24, 2024

Python Data Type: String

Overview

  • Strings are a data type in Python.
  • Characters enclosed in single or double quotes.

Topics Covered

  • Creating strings.
  • Converting string cases (lowercase, uppercase).
  • Finding the length of strings.
  • String operations in bioinformatics (e.g., DNA sequences).

Resources

  • Notebooks available on Patreon.
  • Google Colab for running code online.

Creating Strings

  • Define a string variable: my_string = "bioinformatics".
  • Use single or double quotes.
  • Print strings using print() function.

String Operations

Creating DNA Sequences

  • DNA sequences as strings: my_dna = "AGC".

Converting Cases

  • Convert to lowercase: my_string.lower().
  • Convert to uppercase: my_string.upper().
  • Capitalize first letter: my_string.capitalize().

Finding Length

  • Use len() function to find number of characters.
  • Spaces are counted as characters.

Checking Substring

  • Check presence: 'p' in my_string.
  • Case sensitive checks.
  • Find index: my_string.find('love').

Counting Characters

  • Count occurrences: my_string.count('o').
  • Use for biological sequences like DNA.

Replacing Characters

  • Replace substrings: my_string.replace('g', 'G').
  • Convert DNA to RNA by replacing T with U.

Combining Strings

  • Concatenation using + operator: full_name = first_name + " " + second_name.
  • String formatting methods:
    • "%s %s" % (first_name, second_name)
    • "{} {}".format(first_name, second_name)

Indexing and Slicing

  • Access specific characters or sub-strings.
  • Indexing: Start from zero.
  • Slicing: Use my_string[start:end].
  • Negative Indexing: Access from the end.
  • Stepping: my_string[start:end:step].

Bioinformatics Applications

  • DNA sequence manipulation using strings.
  • Calculate GC content.

Exercises

  • Length of sequence.
  • Percentage GC content.
  • Check nucleotide position.

Conclusion

  • Practice is key in mastering Python string operations.
  • Explore additional Python string functions and methods.
  • Utilize Python for bioinformatics and biological data handling.

For further exploration, consider looking into regular expressions for more complex string manipulation tasks.