len("")
0
Reading: Think Python Chapter 8
You have used strings for some basic operations: creating them, concatenating two strings with +
, and printing them. Strings in Python are substantially more powerful data structures.
''
'a'
is a character'A'
is a different character' '
(space) is also a characterlen
on the string:We can also retrieve individual characters from a string, based on the position of the character.
Counting in Python starts at 0. Just as the range
function starts at 0 by default, the first character of a string is at position 0.
Pay attention to the syntax: the string, followed by an integer in square brackets. The square brackets indicate we are indexing the string; the integer is the position.
Note the variable assignment and indexing on the variable name.
Strings can also be sliced: rather than retrieving a single character, a substring is retrieved. Slicing uses two or three integers: a start position, a stop-before position, and a spacing. This is very similar to range
.
Master the basic slicing syntax first.
We will use a simple list for these examples:
x[1:]
or x[:5]
x[:5]
yields [1, 2, 3, 4, 5]
x[2:]
yields [3, 4, 5, 6, 7, 8]
-1
is the last element, -2
is the second-to-last, etc.x[-2]
yields 7
x[2:-2]
yields [3, 4, 5, 6]
(the second-to-last element, 7, is not included)x[-5:]
yields [4, 5, 6, 7, 8]
x[1:5:2]
yields [2,4]
x[3:0:-1]
yields [4, 3, 2]
x[1:5:-1]
yields []
- an empty list - going backwards from 1 already starts out after the stopping point!x[::-1]
yields [8, 7, 6, 5, 4, 3, 2, 1]
x[::]
yields [1, 2, 3, 4, 5, 6, 7, 8]
- but it’s faster to just use x
🙃Expressions involving strings conform to the same rules we have seen for expressions: they are components that can be used to compose larger expressions and assign values to variables.
len
returns an integer and can be used wherever integers are usedStrings contain built-in functions associated with the string. These associated functions are called methods.
An example is the <str>.upper()
method. Note the syntax: <str>
indicates the method is available for any string:
for
and while
loopsWe can combine these techniques to iterate over characters in a string:
All of the syntax here is syntax you have already learned!
We could also do this with a while
loop:
What is shown above iterates the loop through values and is often called “value iteration.”
It’s also possible to iterate directly over the characters in a string. This can only be done with a for
loop, and is called “content iteration” (because it iterates through the string’s content).
The syntax is very simple:
Strings in Python cannot be changed. They are immutable.
We have seen string concatenation:
The result of concatenation is a new string.
If we were to try to reassign an individual character of a string, we would get an error:
Instead of changing a string, we can compose a new string using characters from the old string:
in
The in
keyword between two strings will return True
or False
depending on whether the first string is in the second string:
This is a very different use of in
than you have seen in for
loops. Trace through this example to see the difference. Note how the conditional is nested inside the loop!
Write a function one_star
that takes one argument, a string.
Your function should return a new string, identical to the argument but with the second character changed to the '*'
character.
starred_out('vote')
returns 'v*te'
starred_out('12345')
returns '1*345'
Write a function many_stars
that takes one argument, a string.
Your function should return a new string, identical to the argument but with every character except the first and last changed to the '*'
character.
many_stars('vote')
returns 'v**e'
many_stars('12345')
returns '1***5'
Write a function shorter_string
that takes two arguments, both strings, and returns whichever string is shorter, or the first string if both are the same length.
shorter_string('too much', 'many')
returns 'many'
shorter_string('same', 'okay')
returns 'same'
shorter_string('vowel', 'consonant')
returns 'vowel'
Write a function shared_character
that takes two arguments, both strings, and returns True
if the strings have at least one character in common, or False
if they have no characters in common.
shared_character('true', 'story')
returns True
shared_character('no', 'dice')
returns False
shared_character('blue', 'lagoon')
returns True
shared_character('yes?', 'how?')
returns True