Working With Strings
Programs frequently have to process text data. Python has several built-in string methods and functions that help do things like convert to upper case, find parts of strings, count the number of characters in a string, and more.
Learning Objectives
You should be able to use Python to:
- Determine the length of a string
- Make a string uppercase
- Make a string lowercase
- Find the first character in a string
Video Walkthrough
Use this video to follow along with the steps in this lab.
Methods and Functions
In Python, strings, integers, and floats are types of objects that hold data. Functions and methods can perform actions on these objects. The difference between a function and a method is that a method is attached to an object, while a function is not. For now, the main difference between a method and a function is how you write the code: do you either 1) just write the name of a function (like print('hi')), or 2) write the name of an object, a period, and then the name of a method (like 'hi'.upper()).
Imagine that you had a Pythonic banana. The banana could have methods attached it, such as peel(), gobrown(), and ripen(). These methods would be called on the banana object to perform actions on the banana. They are called by putting the name of the object, then the name of the function and parentheses. The following is not real Python code, but it demonstrates the concept of calling an object's methods.
banana.peel()
banana.gobrown(time=5)
banana.ripen()
Notice that the methods are called on the banana object with a period between the object and the method name.
Functions, on the other hand, are not attached to objects. They are standalone and can be called on their own. Imagine a hypothetical eat() function that could be called on the banana object or other types of food (i.e., objects).
eat(banana)
eat(hamburger)
eat(orange)
Functions are called on their own, meaning that you can just use the function name (i.e., not preceded by an object and period). The following real Python code shows several common functions.
print("Hello, world!")
str(5)
int("44")
float("3.14")
Both methods and functions can accept parameters, or inputs, to perform actions. These parameters are passed in the parentheses after the method or function name. For example, the print() function can take strings, integers, or other object types as a parameter to print to the console. Some methods and functions can take multiple parameters, separated by commas. Other methods and functions do not take any parameters, so the parentheses are empty.
It just takes experience and exploring the documentation to learn what methods are available for different objects. The following sections will demonstrate some of the most common string methods, how to use the len() function to count the number of characters in a string, and how to access characters in a string by index.
String Methods and Functions
- Open a terminal and run Python (without any arguments) to start an interactive shell.
python
-
Remember that in the interpreter, you can type Python code and see the results immediately without having to use the
print()function. In a standalone Python program, you would need to use theprint()function to see the output when the program runs. -
Strings have several methods, such as the
upper()method that makes a string upper case. Run the following code to use theupper()method. Notice that the string object is followed by a period and the method name because the method is attached to the string object.
"hello".upper()
- The code produces,
HELLO. - Likewise, there is a method to make the text lowercase. You can probably guess what it is after learning about the
upper()method. Yes, it'slower().
"Hi tHeRe".lower()
- The code produces 'hi there'.
- You can count the number of characters in a string with the
len()function. Thelen()function can take a string as a parameter. Notice that thelen()function is not attached to the string object like the methods are. Thelen()function can take different objects as inputs.
len("Jackson")
- The code outputs 7, meaning that there are 7 characters in the name "Jackson."
- You can get different characters in a string based on the index. In Python, indexes start at 0. So the first letter in the word "apple" would be the letter "a" at index 0. Square brackets after the string are used to capture the index.
"apple"[0]
- The code will output the character 'a'.
- The word "apple" has 5 characters, and indexes start at zero, so the letter "e" should be at the 4th index position.
"apple"[4]
- Yep. It's "e."
- Want to live dangerously? Push the boundaries. What is at index 5 in the word "apple"?
"apple"[5]
An error. That's all. You have failed to peer beyond the edge of the universe, unfortunately.

Python can do a quick title case with the title() method.
"i was never very good at punctuation".title()
The Python replace() method will substitute one string for another.
"I messed teh things up".replace("teh", "the")
To combine two strings, you can use the + character as if you were adding numbers.
"one" + " plus " + "two"
The code produces 'one plus two'. Be sure to include the spaces around the word "plus" for the resulting string to be readable.
There are many other things that you can do with strings, but the methods described above are some of the most useful.
Reflection
- How could string methods be used to clean up large data sets?
- How could the combination of
len()help you avoid looking for an index that goes out of bounds?
Key Terms
- Python String Functions and Methods: Built-in functions and methods in Python that allow for manipulation and processing of string data. These functions can perform a variety of operations such as searching, replacing, splitting, and formatting strings. Examples include
len(),str(),upper(),lower(),split(), andreplace(). - Python String Concatenation: The process of joining two or more strings end-to-end to form a single string. In Python, string concatenation can be achieved using the
+operator.