Skip to main content
Chemistry LibreTexts

5: String Class

  • Page ID
    430698
  • \( \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}}} \)

    Overview

    • Strings use single, double or triple quotes
      • Triple quote strings span multiple lines
      • Represent characters not numbers ("1 proton" vs 1 "proton"+
      • Strings can be concatenated ("1"+"1" becomes "11")
    • Strings are Ordered
      • Strings can be indexed
      • Strings can be reverse indexed
    • Strings are immutable
      • Can not change a string (does not support item reassignment)
      • Can be reassigned by slicing and concatenation

     

    Formatting Strings

    When you print a string there are a variety of format options.  I personally tend to use the f-string, but you need to be prepared to read code that uses any of the options.

    Modulo (%) method

    .format() method

    f-string method

     

    String Methods

    String methods are functions that are built into the string object class. The following are some string methods  See Python.org string section for a comprehensive list of string methods.  Note, these should be quickly gone through and then used as references, and some of these involve classes like lists which are covered later in this section.

     

    • str.capitalize()
      Lets analyze the code below. In the first line we assign the string "chemistry class" to a variable called name.  Note the string is in parenthesis, but the name is not.  Now we want to print the variable name but capitalize the first letter of the first word, and so we can use the capitalize method, which is a function of the class string, by typing .capitalize() after the string variable name.  Note print is a function and so we place the argument (name) in it, and capitalize is a method (function of the class string), and so it also needs parenthesis, but it operates on the variable it is appended to. 
    name = "chemistry class"
     
    print(name.capitalize())

    If you wanted to capitalize the first letter of every word you would use the title method that is demonstrated below.

    • str.find()
      note, this method finds the index number of the string, which is an ordered list.  In python index numbers start with zero, and so the word "best" starts at the 17th index position of the string.
    text = 'Chemistry is the best course'
    
    print(text.find('best'))

     

    • str.join (iterable)
      note the variable text is a list of strings and we cover lists in the next section. Here we are using the space string to join each of the words of the list into a stingle string.  After you run the following, remove the blank spot in space, that is, convert " " to "" and then rerun the code.
    text = ['Chemistry', 'is', 'the', 'best', 'class']
    space = " "
    
    print(space.join(text))

     

    • str.lstrip ([chars])
      striping characters can be very important, especially if you import a file with a bunch of characters you do not want.  Data transmitted over the web is usually a string and you often need to convert it to a number by removing the quotes.  That is, 1 is an integer and "1"  or '1' are strings.
    text = ",,,,,Hello world"
    
    x = text.lstrip(",")
    
    print(x)

     

    • str.replace ("old", "new")
    text = 'Ozone Oxygen Butane'
    
    new_text = text.replace('Butane', 'Hydrogen')
    
    print(new_text)

     

    • str.rsplit(separator, max split)

    Converts string into list of strings from the right, with the max split number indicating how many items in the list are created to the right.

    text = "Chemistry,Physics,Calculus"
    
    x = text.rsplit(",", 2)
    
    print(x)

     

    • str.rstrip([chars])
    text = "Pythonnnnnn"
    print(text.rstrip('n'))

     

    • str.split(sep=None, maxsplit=-1)
    text = 'This programming langauge is cool'
    
    x = text.split(None,-1)
    print(x)
    
    x = text.split(None,1)
    print(x)
    
    x = text.split(None,2)
    print(x)

     

    • str.title()
    text1 = 'python programming class'
    print(text1.title())
     
    text2 = 'pyTHoN proGraMMinG clAsS'.title()
    print(text2.title())

     

     


    This page titled 5: String Class is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?