
An Introduction to Python – Part 3 – Amsys
Welcome to the third part of our introduction to Python. If you haven’t done so already, you take a look at the previous parts – Part 1 here and Part 2 here.
In part 3, we are going to look at strings.
Strings
In any script or program you may write, strings are very important. They can be used for a number of purposes, from presenting some textual information, to storing textual information such as a file path.
- You can assign a string to a variable a follows.
myName = "Richard"
print myName
- As mentioned in part 2 you can join two or more strings together by using the + operator to create a more complex string.
greeting = "Hello " + myName
print greeting
filePath = "/Library/Preferenes/somefile.plist"
print filePath
- Strings can contain any number of characters. You could even create an empty string.
emptyString = ""
- If you want to count how many characters in a string we can use the len function.
totalChars = len(greeting)
print totalChars
- Sometimes you may want to create a string that includes some other type of data, for instance a number. We can use the str function to convert a number to string.
print "Total Characters in greeting is " + str(totalChars)
- If you want to find the number of occurrences of a character(s) in a string you can use the count function. This example counts the number of assurances of the letter “l”. This is a case sensitive.
howManyEs = greeting.count("l")
print howManyEs
- If you want to find out the position of a character(s) within a string you can use the find function, which is case sensitive. Each character in a string has an index number, the first character is index 0, the next index 1 and so on.
postionOfR = greeting.find("R")
print postionOfR
- The previous two examples are case sensitive. To overcome these types of issues you can convert a string to either all uppercase or lowercase by using either the lower or upper function.
shouting = greeting.upper()
print shouting
whisper = greeting.lower()
print whisper
- You can replace a set of characters within a string with another set by using the replace function. This example replaces all instances of a “-” and replaces it with a space.
stringWithCommas = "A-B-C-D-E-F-G"
stringWithSpaces = stringWithCommas.replace("-" , " ")
print stringWithSpaces
With the find function we mentioned indexes, that is every character in a string has an index number, with the first character starting at 0. We can make use of these indexes to grab a range of characters from a string.
We create a range by specifying a starting index and an end index. However, the range created would be one less than the end index we specified. The syntax of the range would be as follows:
[ startIndex : endIndex ]
- For example, to grab the first three characters of a string we could use the example below. We create a range from character 0 to character 3.
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
first3Chars = alphabet[0:4]
print first3Chars
- You can create a range from anywhere inside the string. This grabs a set of characters from the middle.
middleChars = alphabet[10:17]
print middleChars
This is the end of part 3. Hope you enjoyed it. Part 4 will follow shortly.