Use an index to get a single character from a string.¶
The characters (individual letters, numbers, and so on) in a string are ordered. For example, the string
'AB'is not the same as'BA'. Because of this ordering, we can treat the string as a list of characters.Each position in the string (first, second, etc.) is given a number. This number is called an index or sometimes a subscript.
Indices are numbered from 0.
Use the position’s index in square brackets to get the character at that position.
atom_name = 'helium'
print(atom_name[0])
{: .language-python}
h
{: .output}
Use a slice to get a substring.¶
A part of a string is called a substring. A substring can be as short as a single character.
An item in a list is called an element. Whenever we treat a string as if it were a list, the string’s elements are its individual characters.
A slice is a part of a string (or, more generally, any list-like thing).
We take a slice by using
[start:stop], wherestartis replaced with the index of the first element we want andstopis replaced with the index of the element just after the last element we want.Mathematically, you might say that a slice selects
[start:stop).The difference between
stopandstartis the slice’s length.Taking a slice does not change the contents of the original string. Instead, the slice is a copy of part of the original string.
atom_name = 'sodium'
print(atom_name[0:3])
{: .language-python}
sod
{: .output}
Use the built-in function len to find the length of a string.¶
print(len('helium'))
{: .language-python}
6
{: .output}
Nested functions are evaluated from the inside out, like in mathematics.
Python is case-sensitive.¶
Python thinks that upper- and lower-case letters are different, so
Nameandnameare different variables.There are conventions for using upper-case letters at the start of variable names so we will use lower-case letters for now.
Use meaningful variable names.¶
Python doesn’t care what you call variables as long as they obey the rules (alphanumeric characters and the underscore).
flabadab = 42
ewr_422_yY = 'Ahmed'
print(ewr_422_yY, 'is', flabadab, 'years old')
{: .language-python}
Use meaningful variable names to help other people understand what the program does.
The most important “other person” is your future self.
Swapping Values¶
Fill the table showing the values of the variables in this program after each statement is executed.
# Command # Value of x # Value of y # Value of swap #
x = 1.0 # # # #
y = 3.0 # # # #
swap = x # # # #
x = y # # # #
y = swap # # # #
{: .language-python}
Solution¶
# Command # Value of x # Value of y # Value of swap #
x = 1.0 # 1.0 # not defined # not defined #
y = 3.0 # 1.0 # 3.0 # not defined #
swap = x # 1.0 # 3.0 # 1.0 #
x = y # 3.0 # 3.0 # 1.0 #
y = swap # 3.0 # 1.0 # 1.0 #
{: .output}
These three lines exchange the values in x and y using the swap
variable for temporary storage. This is a fairly common programming idiom.
{: .solution}
{: .challenge}
Predicting Values¶
What is the final value of position in the program below?
(Try to predict the value without running the program,
then check your prediction.)
initial = 'left'
position = initial
initial = 'right'
{: .language-python}
Solution¶
'left'
{: .output}
The initial variable is assigned the value 'left'.
In the second line, the position variable also receives
the string value 'left'. In third line, the initial variable is given the
value 'right', but the position variable retains its string value
of 'left'.
{: .solution}
{: .challenge}
Solution¶
Numbers are not strings or sequences and Python will raise an error if you try to perform an index operation on a
number. In the [next lesson on types and type conversion](/03-types-conversion/#convert-numbers-and-strings)
we will learn more about types and how to convert between different types. If you want the Nth digit of a number you
can convert it into a string using the str built-in function and then perform an index operation on that string.
a = 123
print(a[1])
{: .language-python}
TypeError: 'int' object is not subscriptable
{: .error}
a = str(123)
print(a[1])
{: .language-python}
2
{: .output} {: .solution} {: .challenge}
Choosing a Name¶
Which is a better variable name, m, min, or minutes?
Why?
Hint: think about which code you would rather inherit
from someone who is leaving the lab:
ts = m * 60 + stot_sec = min * 60 + sectotal_seconds = minutes * 60 + seconds
Solution¶
minutes is better because min might mean something like “minimum”
(and actually is an existing built-in function in Python that we will cover later).
{: .solution}
{: .challenge}
Slicing practice¶
What does the following program print?
atom_name = 'carbon'
print('atom_name[1:3] is:', atom_name[1:3])
{: .language-python}
Slicing concepts¶
What does
thing[low:high]do?What does
thing[low:](without a value after the colon) do?What does
thing[:high](without a value before the colon) do?What does
thing[:](just a colon) do?What does
thing[number:some-negative-number]do?What happens when you choose a
highvalue which is out of range? (i.e., tryatom_name[0:15])
Solutions¶
thing[low:high]returns a slice fromlowto the value beforehighthing[low:]returns a slice fromlowall the way to the end ofthingthing[:high]returns a slice from the beginning ofthingto the value beforehighthing[:]returns all ofthingthing[number:some-negative-number]returns a slice fromnumbertosome-negative-numbervalues from the end ofthingIf a part of the slice is out of range, the operation does not fail.
atom_name[0:15]gives the same result asatom_name[0:]. {: .solution} {: .challenge}