dessert = "chocolate"
topping = 'cherries'
print dessert
print topping
print str(3.1415)
chocolate cherries 3.1415
The function len()
can be used to calculate the length of a string.
len(dessert)
9
The string operators + and * concatenate and repeat a given string. For example, the first line will print combines the two strings chocolate
and cake
to make a new string chocolatecake
. The second line repeats the string chocolate
three times to make the new string chocolatechocolatechocolate
.
print 'chocolate' + 'cake'
print 'chocolate' * 3
chocolatecake chocolatechocolatechocolate
To access a particular character in a string, you can use the bracket operator []
. The index must be an integer. In Python, the first character in a string starts at 0 (not 1). For example, to extract the first element in the string chocolate
:
dessert[0]
'c'
[m:n]
where m is the position to start and n-1 is the position to end (i.e. the slice operator will extract up to but not include the nth position). If the n is missing, then the characters starting from position m to the end of the string are extracted (similar idea if m is missing).[m:n:s]
which is the step size of s between characters. A step size of -1 goes through the word backwards. For example, the following will print 'chocolate' backwards:dessert[::-1]
'etalocohc'
If you want to search if one string is a substring of a second string, use the boolean string operator in
:
print 'late' in 'chocolate'
print 'date' in 'chocolate'
True False
You can also use the not in
operator in the opposite way. The in
operator can be used in conditional statements such as if
/ else
statements.
The operator == (or is
) can be used to test if two strings are equal. The operators <, > can be used to test the alphabetical order of strings.
To traverse through all characters in a given string, you can use for
or while
loops. Here we create the names of the duck statues in the Public Gardens in downtown Boston: Jack, Kack, Lack, Mack, Nack, Oack, Pack, Qack.
prefixes = 'JKLMNOPQ'
suffix = 'ack'
for letter in prefixes:
print letter + suffix
Jack Kack Lack Mack Nack Oack Pack Qack
If you want to compare adjacent letters, you may want to use a while
loop. For example, if you want to determine if a word is a palindrome:
def is_palindrome(word):
i = 0
j = len(word) - 1
while i < j:
if word[i] != word[j]:
return False
i = i + 1
j = j - 1
return True
is_palindrome('tot')
True
There are a set of methods in Python that take in a string and return a value (similar to a function, but different syntax). The syntax is the name of the string followed by a dot (or period) followed by the name of the method.
strip()
= gets rid of the white space in a stringupper()
= take in a string and return the string in all upper case letterslower()
= take in a string and return the string in all lower case lettersfind()
= find all the substrings in a stringFor example, if you want to , use the word return all upper case letters, use the upper()
method. If you want to find all the 'o''s in a word, use find()
:
print dessert.upper()
print dessert.find('o')
CHOCOLATE 2
The find()
method can also take in a starting position and a stopping position of where it should search (but remember the index starts at 0).
split(delimiter)
= splits a string based on a delimiter. If no argument is provided, it splits based on white spaces. If the delimiter is provided as an argument, it will split based on that parameter."Howdy! How are you today?".split()
['Howdy!', 'How', 'are', 'you', 'today?']
The split(delimiter, num)
method can take an optional second argument which is the number of times to split (useful if you want to search a string for a substring and then work with everything before the substring).
ljust(length)
and rjust(length)
= pad the string with spaces on the left and ride side with a given length
replace('potato', 'tomato')
= searches a string and replaces all the words 'potato' with the word 'tomato'.