Strings
Last updated
Was this helpful?
Last updated
Was this helpful?
Types that are comprised of smaller pieces are called collection data types.
Depending on what we are doing, we may want to treat a collection data type as a single entity (the whole), or we may want to access its parts. This ambiguity is useful.
Strings can be defined as sequential collections of characters. This means that the individual characters that make up the string are assumed to be in a particular order from left to right.
A string that contains no characters, often referred to as the empty string, is still considered to be a string.
You can use plus for strings to concatenate two strings. And multiply by a number to add it that many times to itself:
A substring of a string is called a slice. Selecting a slice is similar to selecting a character:
Method
Parameters
Description
upper
none
Returns a string in all uppercase
lower
none
Returns a string in all lowercase
capitalize
none
Returns a string with first character capitalized, the rest lower
strip
none
Returns a string with the leading and trailing whitespace removed
lstrip
none
Returns a string with the leading whitespace removed
rstrip
none
Returns a string with the trailing whitespace removed
count
item
Returns the number of occurrences of item
replace
old, new
Replaces all occurrences of old substring with new
center
width
Returns a string centered in a field of width spaces
ljust
width
Returns a string left justified in a field of width spaces
rjust
width
Returns a string right justified in a field of width spaces
find
item
Returns the leftmost index where the substring item is found
rfind
item
Returns the rightmost index where the substring item is found
index
item
Like find
except causes a runtime error if item is not found
rindex
item
Like rfind
except causes a runtime error if item is not found
equality ==
It turns out, as you recall from our discussion of variable names, that uppercase and lowercase letters are considered to be different from one another. The way the computer knows they are different is that each character is assigned a unique integer value. “A” is 65, “B” is 66, and “5” is 53. The way you can find out the so-called ordinal value for a given character is to use a character function called ord
.
There is also a similar function called chr
that converts integers into their character equivalent.
Often we start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal.
It is often helpful to examine a character and test whether it is upper- or lowercase, or whether it is a character or a digit. The string
module provides several constants that are useful for these purposes. One of these, string.digits
is equivalent to “0123456789”. It can be used to check if a character is a digit using the in
operator.
The string string.ascii_lowercase
contains all of the ascii letters that the system considers to be lowercase. Similarly, string.ascii_uppercase
contains all of the uppercase letters. string.punctuation
comprises all the characters considered to be punctuation. Try the following and see what you get.
Python turns variable into string if they are not strings.
Methods do not change the original string. STRINGS ARE IMMUTABLE.
Other comparison operations are useful for putting words in . This is similar to the alphabetical order you would use with a dictionary, except that all the uppercase letters come before all the lowercase letters.