Flat_Eric Contemplating Data Type Conversions

Built-In Functions and Methods

Built-in Functions

So far, we have learned about several of Python's built-in functions, such as str(), print(), and int(). Python provides a wide variety of these functions; if you follow the link to the official Python documentation, you can see exactly what is available.

From that list, you might recognize some functions we have already touched on, like abs(), min(), or even complex(). These functions, along with others, are used to manipulate numbers like integers and floats. However, we also have functions that are specifically designed for strings.

Functions for Strings

We previously learned that numbers (integers and floats) have specific functions used to manipulate them, like round() or pow(). Similarly, there are functions specifically designed for strings. One of the most useful is len(), which stands for length.

Can you guess how it works? Of course you can! If we run print(len('mississippi')), we will see the number 11 in the terminal.

The "Zero" Confusion

There is something important to be aware of when using len(). If you count the characters in "mississippi," there are 11 characters, and the output shows 11.

Wait—didn't we learn that Python starts counting at zero?

It is easy to get confused here, but there is a clear distinction:

Think of it like this: If you have a physical box of 11 apples, you have 11 apples (the length). But if you were to label them with a marker for Python, the "first" apple would be labeled 0 and the "last" apple would be labeled 10.

Using len() with Slicing

An interesting technique we can use with strings is combining the len() function with slicing.

If we create a variable city = "Bangkok", using the slice print(city[:]) will output Bangkok. However, we can also use the length of the string to define the slice. If we write print(city[0:len(city)]), we still get Bangkok.

This works because the length of "Bangkok" is 7. When we pass len(city) as the "stop" value, Python interprets the command as city[0:7]. Even though the final index is 6, the slice includes everything up to, but not including, the stop index—resulting in the full string.

Try the following:

  1. Create a variable.
  2. Print the length of the variable.
  3. Slice the variable, and print the sliced variable.

Test Your Knowledge

Select the correct answer below to see if you've mastered Expressions and Statements.

Loading quiz...

Built-in Methods

In addition to built-in functions, Python also features built-in methods. While a function has a syntax consisting of a lowercase word followed by parentheses—such as len()—string methods follow a different structure.

In Python, string methods perform specific actions and return new values without changing the original string. You can find a full list of these in the string methods documentation from w3schools.

Methods use a special syntax that usually begins with a dot "." attached to a variable or object, such as .format().

From the documentation, you can see that there are more than 40 string methods currently available. You will likely only use about 10 or 15 of these throughout your coding journey.

However, like many aspects of programming, simply knowing they exist is the most important part. That way, when you encounter a specific problem, you will know where to look or what to search for to find the right tool for the job.

Try the following:

  1. Create a variable named joke='Why did the chicken cross the road?'
  2. joke.upper()
  3. joke.capitalize()
  4. joke.lower()
  5. joke.find('chicken')
  6. joke.replace('chicken', 'egg')

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: The Counter

Objective: Find out exactly how many characters (including spaces) are in the string "Supercalifragilisticexpialidocious". Display this number in your terminal.

Exercise 2: The Perfect Slice

Objective: Create a string variable containing a long sentence. Use a single print statement to slice and display the entire string from start to finish, but you must use a built-in function to determine the "stop" point of your slice.

Exercise 3: The Perfect Slice

Objective: Take the string "i am not shouting" and display it in all capital letters. Then, take the string "I AM WHISPERING" and display it in all lowercase letters.

Exercise 4: Index Hunter

Objective: Search the string "the quick brown fox jumps over the lazy dog" to find the exact position of the letter "q". Display that position number in the terminal.

Exercise 5: The Swap

Objective: Create a variable with the text "Curiosity killed the cat.". Perform a replacement to change "cat" to "dog", but ensure you store this new version in its own separate variable. Print both the original and the new version to show they are different.

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course