Skip to main content
Engineering LibreTexts

6: Strings

  • Page ID
    122350
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    • 6.1: A string is a sequence
      This page explains that in Python, strings are indexed sequences of characters starting from 0, illustrating with the example of the string "banana". It emphasizes that using non-integer indices leads to a TypeError, highlighting the importance of zero-based indexing for string manipulation in Python.
    • 6.2: Getting the length of a string using len
      This page explains the use of the `len` function to find the length of a string, specifically using 'banana' as an example. It addresses errors arising from out-of-range indices, emphasizing that string indices start at zero. To access the last character, it suggests subtracting one from the length or applying negative indexing, which provides a convenient way to access characters from the end of the string.
    • 6.3: Traversal through a string with a loop
      This page discusses string traversal, explaining how to process characters individually using while and for loops. It emphasizes that the loops continue until reaching the string's length and suggests a reversing exercise.
    • 6.4: String Slices
      This page explains string slicing, where specific segments of a string can be selected using indices. The first index is inclusive and the second is exclusive; omitting either extends the slice from the start or to the end. An empty string is produced if the first index is greater than or equal to the second. The notation fruit[:] represents the full string.
    • 6.5: Strings are immutable
      This page explains that strings in Python are immutable, meaning their characters cannot be changed directly. Attempting to do so results in a TypeError. Instead, to modify a string, you must create a new one by concatenating changes with parts of the original. An example provided illustrates how to change 'Hello, world!' to 'Jello, world!' by combining the new character with the original string's remainder.
    • 6.6: Looping and Counting
      This page outlines a Python program that counts the occurrences of the letter 'a' in the string 'banana' by using a counter pattern. It initializes a count variable and increments it whenever 'a' is found, then prints the total count. The text also recommends creating a function called 'count' that takes a string and a letter as parameters to enhance the program's adaptability.
    • 6.7: The in operator
      This page explains the boolean operator "in," which checks if one string is a substring of another. It returns True if found and False otherwise, illustrated with examples using the strings "a," "seed," and "banana."
    • 6.8: String Comparison
      This page explains comparison operators for strings in Python, highlighting string equality and alphabetical order. It points out that uppercase letters take precedence over lowercase ones, potentially causing unexpected outcomes. To address this, it advises converting strings to a consistent format, such as all lowercase, for reliable comparisons.
    • 6.9: String Methods
      This page explains that strings in Python are objects with data and methods, highlighting the use of the `dir` function to list methods and the `type` function to identify object types. It provides examples of method calls using dot notation, such as `word.upper()`, `word.find('a')`, and methods like `strip` and `startswith`. Additionally, it encourages users to explore the `count` method through an exercise, emphasizing the significance of string manipulation and casing.
    • 6.10: Parsing strings
      This page explains how to extract the domain from an email address by utilizing the `find` method to identify the positions of the at-sign ('@') and the next space. It demonstrates string slicing to obtain the domain, using the example of extracting "uct.ac.za". Documentation for the `find` method is also mentioned.
    • 6.11: Format operator
      This page explains the use of the format operator % in Python for constructing strings by replacing parts with variable data. It details format sequences like "%d" for integers, "%g" for floats, and "%s" for strings, and notes that multiple sequences need a matching tuple. A TypeError arises if the number or type of tuple elements doesn't match the format sequences. While versatile, the operator can be complex. Additional information can be found in the Python documentation.
    • 6.12: Debugging
      This page emphasizes the importance of anticipating user input issues in programming to prevent crashes, using an example of a Python program that fails on empty input. It offers two solutions: employing the `startswith` method to handle empty strings safely, and using the guardian pattern to conduct checks based on string length. These strategies help create robust and user-friendly applications.
    • 6.E: Strings (Exercises)
      This page provides exercises on string manipulation in Python. It includes Exercise 5, which focuses on using the `find` method and string slicing to extract and convert a portion of the string 'X-DSPAM-Confidence:0.8475' into a float. Exercise 6 suggests reviewing Python's string methods, experimenting with `strip` and `replace`, and clarifying optional parameter syntax in the documentation. The text also hints at a future discussion on tuples.
    • 6.G: Strings (Glossary)
      This page defines key programming concepts such as "counter," "empty string," and "format operator," alongside terms like "flag," "invocation," and "immutable." It explains "index," "item," and "method" related to data structure manipulation, as well as "search," "sequence," "slice," and "traverse" for navigating collections.


    This page titled 6: Strings is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform.