Section 2: Strings

Welcome to Section 2! In this section, we will cover Python strings. Let's get started!

2.1 Strings

A string is just a sequence of characters that can be comprised of letters (any case), numbers, spaces, and/or symbols. Here are some examples of strings ...

example_1 = "My favorite number is 10!"

example_2 = 'Your favorite number is 1!'

Strings need to either be put between single quotes ' ' or double quotes " ". I will use double quotes, but both are okay. However, you cannot mix and match them! Both lines above will be saved to their respective variables and will be stored as strings.

Every value in a string has a place value called an index. The leftmost character in a string has an index of 0. The character to the right of that has an index of 1. This pattern continues until the end of the string. The highest index of a string is one less than the length of the string as the first index is 0 instead of 1.

Suppose you want to find the character in a string at a certain index. Thankfully, there is an extremely easy way to do that called indexing. Here are some examples of indexing ...

alphabet = "abcdef"

print(alphabet[0])

print(alphabet[3])

The code above will print two numbers from the variable, alphabet. The second line uses the index 0, so it will print "a", the first number. The third line uses the index 3, so it will print "d", the 4th number. As you can see, the syntax for indexing is quite simple. You first need the variable's name or the string typed out. Then, you have to put the index you want inside square brackets directly after the variable's name or the string.

Even though the previous type of indexing may seem good enough, there are a few more advanced methods I will show you. These methods are called slicing. You will be using this throughout the course. Here are the three main types of string slicing ...

alphabet = "abcdef"

print(alphabet[1 : 5])

print(alphabet[: 4])

print(alphabet[2 :])

As you can see, the syntax is similar but not exact. The only difference is that there is two numbers and a colon inside the brackets. The first number is the starting index, so the second number is the ending index, and the colon separates the two. Just to clarify, if the ending index is n, the last index used will be n - 1. Let's take the the first example. It would print indexes 1, 2, 3, and 4 in a string. This means it would print "bcde". The next example has no start number; this means we will start from the beginning and end before the ending index. This means that the third line would print "abcd". The fourth line has no end number; this means that we will start at the starting value and go until the end. The last line will print "cdef".

Now that you have strings down, let's move on to the next part!

2.2 Type

When we have a variable and want to find the data type of a variable, we can use a function. The function which we will use is called type. Here are some examples ...

example_1 = 1

print(type(example_1))

print(type("Type?"))

The code above will first print <class "int">. This just means the class (type) of the variable is integer. The second line of output will be <class "str">. This just means the class (type) of the character sequence is a string. The syntax of type is simple. You need to start by typing type(...). You will then type either a variable's name or any data type in the parentheses.

Now that you have type down, let's move on to the next part!

2.3 Str

We will often have an integer or float that we need converted into a string. Thankfully, there is a fnction called str to help us do that. Here are some examples ...

example_2 = str(2)

print(type(example_2)

print(type(str(3.33)))

This code will print <class "str"> two times. This means that our first integer was successfully converted into a string; it is "2". This also means that our second float was successfully converted into a string; it is "3.33". The syntax for str is simple. You need to start by typing str(...). You will then type wither a variable's name or any data type in the parentheses.

Now that you have str down, let's move on to the next part!

2.4 Escape Sequences

When we are coding, sometimes we want to have an indentation, new line, or special character in the middle of a string. Escape sequences are the way to do that. Before we start, the main rule about escape sequences is that they always start with a backslash. The backslash, \, is commonly used in coding and can be found on the top right of your computer. Here are some examples ...

print("There \t are \t some \t tabs.")

print("There \n are \n some \n new \n lines.")

print("There \\ are \\ some \\ backslashes.")

Without the \ and what follows them, you would each of the three lines to print "There are some (tabs, new lines, backslashes).", respectively. However, the first line of output has a tab between each word. The second line of output has a new line between each word. the third line of output has a backslash between each word. These shortcuts are used to save time and effort for you. They also keep your code much shorter.

There are many more escape sequences that you can research on your own as well.

Now that you have escape sequences down, let's move on to the next part!

2.5 Input

The ultimate goal when we are coding is to do something for a user. Coders often want to take the input of a user to complete their code. Fortunately, there is a simple function to do that for us. Here is an example ...

name = input("Name please: )

print(name)

The code above would first output the prompt "Name please: ", and it would let you type an answer after the string. Your answer can be anything and will be converted into a string. When you press enter, your answer will be saved to name and will be printed in the last line. If I typed in "Bob", it would print "Bob". The syntax of input is rather simple. You need to type "input(...)" and type a string prompt inside the parentheses if you like.

Now that you have input down, let's move on to the next part!

2.6 Int

Whenever we have an integer which is in a string and want to convert it into an integer, we can use the int function. Here is one example ...

number = input("Number please: ")

print(num)

print(int(num))

The first line is the same line as the one in the previous part. It saves the users input to the variable num as a string. The second line will print the input as a string type. The last line uses the function int, so it will output the input as an integer type. The syntax of int is the same as most of the functions we have covered already. You have to type "int()" and put a string or string type variable's name between the parentheses. When you are using int, the code will output a ValueError if the string does not have only an integer in it.

Now that you have int down, let's move on to the next part!

2.7 Float

Similarly to the int function, the float function helps you convert a float that is in a string into a float. Here is one example ...

number = input("Float please: ")

print(num)

print(float(num))

The first line is the same line as the one in the previous part. It saves the users input to the variable num as a string. The second line will print the input as a string type. The last line uses the function float, so it will output the input as an float type. The syntax of float is the same as most of the functions we have covered already. You have to type "float()" and put a string or string type variable's name between the parentheses. When you are using float, the code will output a Value Error if the string does not have only a float in it.

Now that you have float down, let's move on to the next section!

Sample Problems