Jump To
- String Basics
- Slicing
- String Methods: Information About Strings
- String Methods: Working With Substrings
- String Methods: Basic Formatting
- Advanced String Formatting
String Basics
Recall that a string is a sequence of characters. These characters may be letters, numbers, whitespace, or special characters. We have already seen how we can concatenate two strings using the + operator, or repeat a string several times using *.
>>> print("tro"+"lo"*3)
trolololo
Often, we would like to know how long a string is. The len function returns the length of a given string, including all whitespace.
>>> s1 = "four" >>> s2 = " seven" >>> print(len(s1), len(s2)) 4 7
Each character in a string is in a certain position, known as an index (plural indices). For instance, consider the string “CODE”. The index of the letter C is 0, since it is the first character. It is not 1, as some might expect — Python uses zero-based indexing for all sequence types, including strings and (as we will see later) lists. The letter E is the last letter, and has an index of 3. Note that the index of the final character is one less than the length of the string. Trying the following code produces an error.
>>> s = "123"
>>> print(s[len(s)])
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
print(s[len(s)])
IndexError: string index out of range
This is because there are only three characters in the string, and we have asked for index 3. Since the three characters are in indices 0, 1 and 2, there is no index of 3. Note that if we try the following, we get some strange behaviour.
>>> s = "123" >>> print(s[-1]) 3
In this case, the negative tells Python that it should move backward through the string by one index. We will talk a bit more about this later, as there are some interesting applications that can be solved by traversing strings backwards.
Here’s another example of indexing. The following code prints all of the letters of “CODE” using their indices.
s = "CODE" for x in range(len(s)): print(s[x])
Using len and indexing, we can handle loops using arbitrary-length strings.
n = input("Integer: ")
print("The digits in the number are ", end="")
for count in range(len(n)-1):
print(n[count]+", ", end="")
print("and", n[len(n)-1])
Data can be stored using a variety of encodings. One of the original character encoding schemes was ASCII (American Standard Code for Information Interchange), which uses the values 0-127 to represent letters in the Roman alphabet (both upper- and lowercase), the digits 0-9, as well as special characters such as punctuation, spaces, and so forth. While ASCII has been replaced by other encodings (such as Unicode), it is still widely-used in many programming languages. Click here to see a full chart.
Python uses ord to obtain the ASCII integer for a particular character, and chr to obtain the character given an integer. Below is an example of each.
>>> ord("A")
65
>>> chr(65)
'A'
Related to strings are substrings. A substring is a smaller string made up of adjacent characters taken from a larger string. For instance, “cat” is a substring of “catatonic”, whereas “dog” is not a substring of “dropping”, even though “dropping” contains all of the letters in “dog”.
A good way to check if a string contains a particular substring is to use in, which returns True if the substring is found, and False if it is not.
s = "This is a string."
if "a" in s:
print("The string contains an 'a'.")
if "z" in s:
print("The string contains a 'z'.")
This can be handy for checking for specific inputs, without the use of and or or operators.
letter = input("Please enter a letter: ")
if letter in "aeiouAEIOU":
print("You entered a vowel.")
else:
print("You entered a consonant.")
The in command is also useful for looping through all elements of a string. For example, the program below prints the values A1, A2, A3, B1, B2, B3, C1, C2 and C3, similar to how cells would be named in a spreadsheet.
for column in "ABC":
for row in range(1, 4):
print(column + str(row))
There are plenty of useful applications of in. We will explore some of these shortly.
Exercises
- Write programs to solve each task.
- Determine if a letter entered by the user is a letter in “python”.
- Determine if a given string consists entirely of numbers.
- Count the number of double-vowels (e.g. “ee”) in a given word.
- Print your name using the numeric ASCII values.
- Given a series of ASCII values, print the corresponding string.
- Read n words, and determine the longest and shortest words.
- Read n words, and determine the total number of letters entered in all words.
- Given a user-entered word, print the first and last letters.
- Print every other letter in a given word.
- Check if a given string is a palindrome, which is a word that reads the same forward or backward.
Slicing
Exercises
- Write programs to solve each task.
- Given a string, s, construct a palindrome made up of s and its reverse.
- Given a string, s, return a new string composed of every third character of s.
- Read a string, s, and an integer, n, from the user and return a new string composed of the first and last n characters of s. Be sure to handle cases where the user enters an inappropriate value of n.
- Determine the number of positive integers below 1,000,000 that are palindromes.
- Insert an x before the first occurrence of an a in a string. If there are no as in the string, output a message indicating this.
- Insert an x before each occurrence of an a in a string. If there are no as in the string, output a message indicating this.
String Methods: Information About Strings
Exercises
- Write programs to solve each task.
- Determine if a given string is either composes entirely of numbers, or entirely of letters.
- Determine if a given word has a prefix un- and a suffix -ing, such as the words unforgiving or unrelenting.
- Determine the number of non-alphanumeric characters in a string.
- Count the number of capital and lowercase letters in a string.
- Count the number of numbers and letters (either capital or lowercase) in a string.
- Read n strings, and determine the average number of capital letters per string.
- Write a function isInteger that determines if a string, s, can be typecast to an integer. An integer is defined as any whole number (no decimal point), with an optional negative sign as its first character. Return True if the string is an integer, and False if not.
String Methods: Working With Substrings
Exercises
- Write programs to solve each task.
- Count the number of words that contain the substring “and” at least once.
- Count the number of double vowels in a string.
- Count the number of uppercase vowels in a string.
- Given a string, print the new string formed by omitting all non-alphabetic characters.
- Count the number of words that contain all distinct characters.
- Count the number of times the substring “ana” occurs in a word, if the substring may occur more than once, or may overlap. For example, “banana” contains 2 such substrings.
- A pandigital number is an n-digit positive integer that contains the digits 1-n in some order. For example, 2143 and 52134 are pandigital. Read n integers from the user and count the number of pandigital numbers entered.
String Methods: Information About Strings
s.islower() – Returns True if all letters are lowercase, False otherwise.
s.isupper() – Returns True if all letters are uppercase, False otherwise.
s.istitle() – Returns True if the first letter of each word is capitalized, False otherwise.
s.isdigit() – Returns True if all characters are numeric, False otherwise.
s.isalpha() – Returns True if all characters are alphabetic, False otherwise.
s.isalnum() – Returns True if all characters are letters or numbers, False otherwise.
s.isspace() – Returns True if all characters are whitespace, False otherwise.
s.endswith(characters) – Returns True if the string ends with the specified characters, False otherwise.
s.startswith(characters) – Returns True if the string starts with the specified characters, False otherwise.
Exercises
String Methods: Working With Substrings
s.count(substring) – count the number of occurrences of a given substring within a string
s.find(substring)
s.rfind(substring)
s.index(substring)
s.replace(old, new[, max])
Exercises
String Methods: Basic Formatting
s.captitalize – capitalize the first character of a string
s.lower – convert all letters to lowercase
s.upper – convert all letters to uppercase
s.swapcase – switch all lowercase letters to uppercase, and vice versa
s.title – capitalize the first letter of each “word” in a string
s.center(width) – center a string within a given width (padded by spaces)
s.ljust(width) – left-justify a string within a given width (padded by spaces)
s.rjust(width) – right-justify a string within a given width (padded by spaces)
s.lstrip(chars) – strip all indicated characters from the left side of a string
s.rstrip(chars) – strip all indicated characters from the right side of a string
s.strip(chars) – strip all indicated characters from both sides of a string
Exercises
Advanced String Formatting
<STRING>.format(<ARGUMENTS>)
format uses its own “mini-language” to specify how strings should be displayed.
print("x has a value of {}".format(x))