Outside Resources:
TheNewBoston Video – Slicing Strings
Basic Python Tutorial Video – String Variables
Tutorials Point Interactive Tutorial – Strings
String data is text. It’s called “string” because it’s a string of characters. A character is a single item of text, which could be a letter, digit, symbol, or even a space.
A string literal is text in quotes. It’s a literal because it doesn’t represent something else the way a variable does. It literally is what it says it is.
The str() function can convert a number (and some other types of data) to a string. This can be useful for printing. The example below shows how to concatenate a number to a string literal using str().
x = 99 # First example doesn't work: print("The number is " + x) # This fixes the problem: print("The number is " + str(x))
You can concatenate (add) strings together using the + operator.
You can also multiply text using the * operator. Try it!
print("things" * 5)
You can index a string using an integer inside [square brackets]. This will return a single character from the string. You can try it as follows:
text = "This is a sentence." print(text[0]) print(text[4]) x = 3 print(text[x]) text[100]
The last one will give an error, because the string doesn’t have enough characters.
Python uses zero-based numbering, meaning we start counting at 0 instead of 1.
You can also index strings from the end of the string using negative numbers. “-1” is the index of the last character, “-2” is the second last, and so on.
text = "More Text!" print(text[-1]) print(text[-3])
You can slice a string to pull out a range of characters from the string. In other words, this is how you get part of a string.
text = "Two words" print(text[0:2]) print(text[4:]) print(text[:5])
The first number in square brackets is the start character, and the second number is the stop character, except that the last character is not included. So, text[3:6] would give you characters number 3, 4, and 5, but not character number 6.
If you leave out one of the numbers, then the slice will start at the beginning of the string (if the first number is omitted) or continue to the end of the string (if the last number is omitted).
You can also use negative numbers in a slice. For example, you can use something like text[-3 : ] to get the characters from the 3rd last character to the end of the string.
Use the len() function to find how many characters are in a string. This is a very handy function!
word = "amazing" print(len(word))
You can use the in keyword to check if some text is contained in a string.
text = "happy fun time" if "fun" in text: print("fun was found")
The index() function locates text in a string and returns the position where that string begins.
index() is a string function, which means that any string has this function. To call it, you type the name of the string, then a dot, then index(). See the example.
text = "The Eagle has landed." x = text.index('Eagle') print(x) y = text.index('Chicken') print(y)
Notice that when the text (such as ‘Chicken’ above) is not contained in the string, the index function returns a result of -1.
You can split some text into a list of separate words:
sentence = "This is multiple words." word_list = sentence.split() print(word_list[0]) print(word_list[1])
Strings also have the ability to convert themselves to upper or lower case.
words = "RaNDOm CapITALs" print(words.upper()) print(words.lower()) print(words)
# console output for the code above RANDOM CAPITALS random capitals RaNDOm CapITALs
You will notice that the upper() and lower() functions do not modify the original string. Instead, it returns the new version of the string. To modify the original variable, you could do something like this:
words = "RaNDOm CapITALs" words = words.lower()
These upper and lower functions are handy for making user input not case sensitive. Here is an example:
x = input("Do you like ice cream? ") if x.lower() == "yes": print("Cool; you like ice cream.")
In the example above, the condition would be true if the user typed “YES” or “yes” or “yEs”.
When you use that trick, make sure you are comparing to a fully lower case string. Otherwise it will be impossible for x.lower() to equal, for example, “Yes”.
String Functions: strings have many of their own functions .
Try some of the functions from this link:
https://www.w3schools.com/python/python_ref_string.asp