Section 6: Strings Continued

Welcome to Section 6! In this section, we will cover the more advanced functions for strings. Let's get started!

6.1 Upper and Lower

In this part, we are going to explore different functions we can use on strings. The first couple are how to convert a string from lowercase to uppercase and from uppercase to lowercase. Here are some examples ...

lowercase = "lowercase"

uppercase = "UPPERCASE"

print(lowercase.upper())

print(uppercase.lower())

The first two lines set the two variables, lowercase and uppercase, to strings in their respective cases. The third line will print "LOWERCASE", the uppercase version of the variable lowercase. The fourth line will print "uppercase", the lowercase version of the variable uppercase. The syntax for these functions is simple. You just have to add .upper() or .lower() to the end of a variable name to get the uppercase or lowercase version of the variable. Remember, the variable must be a string, but there can be anything inside the string.

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

6.2 Isupper and Islower

Sometimes, we want to check if a variable is uppercase or lowercase. To do that, we can use the isupper() or the islower() functions, respectively. Here are some examples ...

print(lowercase.isupper())

print(lowercase.islower())

print(uppercase.isupper())

print(uppercase.islower())

What this program will do is perform the two functions mentioned above to our two variables, lowercase ("lowercase") and uppercase ("UPPERCASE"). The first line will print False, because lowercase is not in uppercase. The second and third lines will print True, because lowercase is lowercase and uppercase is uppercase. The last line will print False as uppercase is not lowercase. These functions have the same syntax as the .uppper() and the .lower() functions.

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

6.3 Join

Moving on from the two cases, something that we want to know how to do is how to combine multiple strings into one string. To do this, we can use .join(). Here are some examples ...

print("".join(["Hello", "World", "!")

print(" ".join(["Hello", "World", "!")

The first line will print "HelloWorld!". It does this because the string before the .join() is empty. The second line will print "Hello World !". It does this because the string before the .join() has a space in it. The string before the .join() is what is added between each of the strings inside the brackets. The syntax for .join is complicated. You first need to have a string, empty or not empty, and .join(). After that, you have to type in brackets inside the parentheses. Lastly, you will put the strings that you want added between the brackets.

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

6.4 Split

The opposite of joining is splitting. We can use the function .split() to split a string into multiple parts. Here are some examples ...

print("One, Two, Three".split())

print("One, Two, Three".split(", "))

The first line will print ["One,", "Two,", "Three"]. Since there is nothing inside the parentheses, the string will be automatically split by a space. The second line will print ["One", "Two", "Three"]. Since ", " is in the parentheses, the string will be split by ", ". The syntax for .split() is the same as everything else, but there can be a string between the parentheses if the coder wants.

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

6.5 Replace

Coders often want to replace one part of a string with something else. To do this, we can use the replace function. Here is an example ...

print("Good Morning!".replace("Morning", "Afternoon"))

This code will print "Good Afternoon!". What the replace function does is replace the part of the main string that matches the first string in the parentheses, "Morning", with the second string in the parentheses, "Afternoon". The syntax for the replace function is that you need to type .replace() after a string and add two strings, the old string and the new string, between the parentheses.

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

6.6 Len

Coders often want to know the length of a string for various purposes. To do that, we can use the len() function. Here are some examples ...

print(len("coding"))

print(len("This is a string."))

The first line will print 6. It will print 6 because there are six letters in the string "coding". The second line will print 17. It will print 17 because there are 17 characters in the string "This is a string." The syntax for len is a bit different. You have to type len() and put the string you want to check the length of inside the parentheses.

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

Sample Problems