Strings
Strings are another very important data type in programming. A string is simply a set of characters that represent text in our programs. We can then write programs that use and manipulate strings in a variety of ways, allowing us to easily work with textual data.
Strings in Flowcharts & Pseudocode
The table below lists the flowchart blocks used to represent strings, as well as the corresponding pseudocode:
Operation | Flowchart | Pseudocode |
---|---|---|
Create String |
|
|
Access Character |
|
|
String Length |
|
Strings in Python
Let’s review the syntax for working with strings in Python.
String Creation
Strings in Python are declared just like any other variable:
s = "abc123"
Notice that strings are enclosed in double quotations marks "
. Since Python does not have a data type for a single character, we can do the same for single character strings as well:
c = "a"
There are several special characters we can include in our strings. Here are a few of the more common ones:
\'
- Single Quotation Mark (usually not required)\"
- Double Quotation Mark\n
- New Line\t
- Tab
String Parsing
Most of the time, we will need to be able to parse strings in order to read input from the user. This is easily done using Python. Let’s refer to the skeleton code given in an exercise:
# Load required modules
import sys
def main(argv):
# create a file reader for terminal input
reader = sys.stdin
# read a single integer from the terminal
x = int(reader.readline())
# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-
# main guard
if __name__ == "__main__":
main(sys.argv)
This code will initialize a variable called reader
to read input from the terminal, or sys.stdin
in Python.
Once we have a reader initialized, we can read a line of data from the input as follows:
line = reader.readline()
If we know that line will contain a single item of a different data type, such as an integer, we can also convert that input using the appropriate method:
x = int(reader.readline())
Finally, if we have read an entire string of input consisting of multiple parts, we can use the split
method to split the string in to tokens that are separated by a special delimiter. When we do this, we’ll have to use special methods to convert the strings to other primitive data types. Here’s an example:
line = "This 1 is 2.0 true"
parts = line.split(" ")
first = parts[0]
second = int(parts[1])
third = parts[2]
fourth = float(parts[3])
fifth = bool(parts[4])
In this example, we are able to split the first string variable into $5$ parts, each one separated by a space in the original string. Then, we can use methods such as int()
to convert each individual string token into the desired data type.
Reading Input in a Loop
When reading an unknown number of lines of input, we can use a loop in Python such as the following example:
for line in reader:
line = line.strip()
if not line or len(line) == 0:
break
# parse the input
This will read input until either a blank line is received (usually via the terminal), or there is no more input available to read (from a file).
String Operations
There are also several operations we can perform on strings in Python:
s1 = "This"
s2 = "That"
# string length
x = len(s1)
# string comparison
# can use standard comparison operators
b1 = s1 == s2
b2 = s1 < s2
# concatenation
s3 = s1 + " " + s2
Additional methods can be found on the Python Built-In Types: str and Python Common String Operations pages
String Formatting
Strings can also be used to create formatted output in Python through the use of f-strings. Here’s a short example:
sum = 123
avg = 1.23
name = "Student"
print(f"{name}: Your score is {sum} with an average of {avg}.")
When we run this program, the output will be:
Student: Your score is 123 with an average of 1.23.
Each item in the formatted output can also be given additional attributes such as width and precision. More details can be found on the Python Format String Syntax page.