in
Operator
Individual characters in a string may be accessed in exactly the same way that individual elements of a list are accessed: use an integer index to access a character. Unlike lists, strings are immutable, so you cannot change an individual character of a string by referring to its index. Some example:
Code | Result | Description |
---|---|---|
s = "Some sentence." |
"Some sentence" |
String variable creation |
s[3] |
"e" |
Captures fourth character of string |
s[999] |
Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range |
Generates an error |
s[3] = "x" |
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment |
Generates an error: strings are immutable |
A substring, or slice of a string is accessed by assigning two
indexes separated by a ":
". (This is identical to list
slices.) The values returned start with the first index up to, but
not including, the second index. Indexes that are too small, too large, or
left out default to 0 and the length of the list. Some examples:
Code | Result | Description |
---|---|---|
s = "Some sentence." |
"Some sentence" |
String variable creation |
s[0:1] |
"S" |
Captures first character of string |
s[-100:100] |
"Some sentence" |
Captures entire string |
s[2:9] |
"me sent" |
Captures substring |
s[:] |
"Some sentence" |
Captures entire string |
s[5:] |
"sentence" |
Captures second word |
s[-100:0] |
"" |
Empty string |
s[100:-100] |
"" |
Empty string |
Strings can be traversed - 'walked' through from one end to the other - in three ways, in much the same way that lists can be traversed. The following code shows how the contents of the list:
st = "David"
can be traversed:
By index with a while
loop:
i = 0
n = len(st)
while i < n:
print(st[i])
i = i + 1
By index with a for
loop:
for i in range(n):
print(st[i])
By value with a for
loop:
for c in st:
print(c)
All three versions print the contents of st
as:
D a v i d
for i in st:
print(st[i])
i
is being treated as though it is an index, but it's not. It
is the same as the variable c
in the previous example - it is
actually a character in the string st
. What is being done
here is the equivalent of attempting to print:
st["D"] st["a"] st["v"] st["i"] st["d"]
which isn't going to work, because indexes have to be integers, so the
loop dies on the first iteration. We strongly recommend that you
use i
as a variable name for string iteration when it is an
index, but not when it is one of the characters in the string.
Although you can use any variable name you like, using i
for
other than an index variable is potentially very confusing.
Strings can be concatenated: we can to add strings together. Some examples:
Concatenation simply means adding strings together. Strings may be lengthened by adding other strings to them and concatenated by multiplication by using the+
and *
operators
respectively. Some examples:
Code | Result | Description |
---|---|---|
|
String variable creation | |
full_name = name1 + " " + name2 |
"David Brown" | Concatenating string variables and literals |
multi_name = name1 * 4 |
"DavidDavidDavidDavid" | String repetition |
multi_name = (name1 + ", ") * 4 |
"David, David, David, David, " | String concatenation and repetition |
|
"*_*_*_..." | Starts with an empty string Concatenates to the result
string until the condition is met, much like a running total
|
String methods are methods that can be applied to strings. The basic format is:
return_value = string.method()
Note that string method calls do not change the current string: rather they return a new string, as in this example:
Code | Result | Description |
---|---|---|
name = "David" |
String variable creation | |
|
David DAVID |
Creates an upper case version of name in upname ,
original string name is unchanged
|
There are a number of methods for altering string case:
Method | Purpose |
---|---|
upper() |
Returns an upper case version of a string. |
lower() |
Returns a lower case version of a string. |
Some examples:
Code | Result | Description |
---|---|---|
sentence = "This is a sentence." |
"This is a sentence." |
String variable creation |
sentence.upper() |
"THIS IS A SENTENCE." |
Returns upper-case version of sentence , original
variable is unchanged
|
sentence.lower() |
"this is a sentence." |
Returns lower-case version of sentence , original
variable is unchanged
|
There are a number of methods for testing the contents of a string.
These methods return True
or False
depending on
the state of the string they are testing. These methods are:
Method | Purpose |
---|---|
isalnum() |
Are all the string's characters letters or digits? |
isalpha() |
Are all the string's characters letters? |
isdigit() |
Are all the string's characters digits? |
isspace() |
Are all the string's characters space characters? (Inc. spaces, tabs, linefeeds, etc.) |
isupper() |
Are all the string's letters in upper-case? (Non-letters are ignored.) |
islower() |
Are all the string's letters in lower-case? (Non-letters are ignored.) |
Some examples:
Code | Result | Description |
---|---|---|
alphabet = "ABCs" |
"ABCs" |
String variable creation |
alphabet.isalpha() |
True |
String contains only alphabetic characters |
alphabet.isdigit() |
False |
String contains non-digit characters |
alphabet.isupper() |
False |
String is not all upper-case |
"BFG9000".isupper() |
True |
String alphabetic characters are all upper-case |
"BFG9000".isalnum() |
True |
String alphabetic characters are all alphabetic and digits |
" ".isspace() |
True |
String contains only spaces |
There are a number of methods for removing space characters from either side of a string:
Method | Purpose |
---|---|
strip() |
Returns string with both leading and trailing spaces removed. |
rstrip() |
Returns string with trailing spaces removed. |
lstrip() |
Returns string with leading spaces removed. |
Some examples:
Code | Result | Description |
---|---|---|
|
Create a string variable | |
f"|{string}|" |
| some words | |
String is unchanged |
f"|{string}.strip()|" |
|some words| |
Removes the spaces around the string |
f"|{string.rstrip()}|" |
| some words| |
String has rightmost spaces removed |
f"|{string.lstrip()}|" |
|some words | |
String has leftmost spaces removed |
Strings can be searched to see if they contain other strings or substrings. A substring is just another string that may or may not be part of another (usually larger) string. Search methods allow only part of the original string to be searched. This is shown by the syntax
(srch [, start[, end ]])
where srch
is the string to search for, start
is
the starting index within the main string to begin searching, and end
is the last index within the main string to search. This is equivalent to
segmenting a string with the [start:end]
segmentation syntax.
Method | Purpose |
---|---|
count(srch) |
Returns the number of times srch appears in the
string.
|
find(srch) |
Returns the index of the first occurrence of srch in
the string. Returns -1 if srch is not found.
|
startswith(prefix) |
Returns whether the string prefix begins the string (True )
or not (False ).
|
endswith(suffix) |
Returns whether the string suffix ends the string (True )
or not (False ).
|
All counts and searches are case-sensitive. Counts and searches operate on
non-overlapping substrings. Thus "Wooo!"
contains the
substring "oo"
once, not twice. Note that find
returns the index of location of the search string in the main string, not
in any substring that may be specified.
Some examples:
Code | Result | Description |
---|---|---|
name = "David" |
Create a string variable | |
name.count("D") |
1 |
There is 1 "D" in the string |
name.count("d") |
1 |
There is 1 "d" in the string |
name.upper().count("D") |
2 |
There are 2 "D" in the upper-case version of the string |
name[1:4].count("D") |
0 |
There is no "D" in the string "avi" |
name.startswith("D") |
True |
The string begins with "D" - case is significant |
name.find("v") |
2 |
"v"is at index 2 |
name.find("x") |
-1 |
"x"is not in the string |
Strings can be compared in the same way that any other objects can be
compared in Python, with the <, <=, >, >=, ==, !=, in,
not in
operators. These operators return True
or False
.
Strings are compared on the basis of the integer ASCII values of their
component characters. For example, the letter "A"
comes
before "a"
in the ASCII table. Some examples:
Code | Result | Description |
---|---|---|
name = "David" |
String variable creation | |
|
True |
Use case conversion with string comparison |
|
True |
Comparisons can be done with, and saved to, variables |
"Brown" < "Browne" |
True |
Shorter strings come before longer strings that match up to the length of the shorter string |
"a" < "A" |
False |
Upper-case letters come before lower-case letters in comparisons |
in
Operator
The in
operator determines if a specific substring is found
in a string or not, and returns True
or False
respectively. It is used as:
substring in string
Examples:
Code | Result | Description |
---|---|---|
name = "David" |
"David" |
String variable creation |
"a" in name |
True |
name contains "a" |
"z" in name |
False |
name does not contain "z" |
|
True |
Result stored in boolean variable |
|
False |
Result stored in boolean variable |