← Python10 min read
Fun With Strings
Strings are the data type that Python uses for its text. This is the same in most programming languages. There are different methods that you can perform on Strings than you can in other languages.

What Are Strings?
Strings are the data type that Python uses for its text. This is the same in most programming languages. There are different methods that you can perform on Strings than you can in other languages.
Before we get too deep in the methods that you can perform on strings, let’s review how to determine if your variable is of type str. To determine this, you can run the following code:
word = "zebra"
print(type(word))
When you run this code, the output should tell you that the type of our variable, word, will be of type String.
String Formatting
In Python, there are multiple ways to build Strings. As in most languages, the ‘+’ sign can be used to build Strings by adding the different Strings together. But there is also a newer way of formatting with Python.
name = "Ryan"
sentence_1 = "My name is " + name
sentence_2 = "My name is {}".format(name)
print(sentence_1)
print(sentence_2)
The code segment shown above will build 2 different Strings and then print them to the screen. When the code segment is run, the program will print the same thing twice. These methods of combining Strings produce the same results on the final String.
Finding the length of a String
Finding the length of a String can be very useful. Let’s say that you have a String that you want to change by individual letters or print certain letters on the screen. Well you’ll need to know how long the String is so that your loop operates correctly. To do this you’ll want to use the len keyword.
word = "zebra"
length = len(word)
print("The length of " + word + " is " + str(length) + " letters.")
Working with Substrings
With Strings in all programming languages including Python, you can section off part of the String into what are called substrings. Python has a few different ways that you can create substrings; some of these are similar to other common programming languages and some are a little different. Let’s start with just creating a simple substring. Let’s take the String “excitement” and try to define a substring from this that will result in a new String, “excite”.
word = "excitement"
new_word = word[0:6]
The segment shown above is how you can create a substring that starts with the character at index 0 (the first letter in the String) and will go all the way up to the character at index 6, non-inclusive. An easy way to think about how many characters are in the new substring is to subtract the indices: 6 - 0 = 6, therefore there will only be 6 characters in the new substring.
word = "excitement"
new_word = word[:6]
The segment shown above looks very similar to what was done earlier. In fact it will produce the same result. By using ‘:6’, this will load the substring with all the characters from the beginning up to the character at index 6, non-inclusive. The next couple examples are similar to what we just did, but are ways of obtaining the end of the String.
word = "excitement"
new_word = word[6:10]
This segment will create a new substring that will contain the letters ‘ment’. It does this by starting with the character at index 6 and stopping with the character at index 9, which happens to be the last letter in the String.
word = "excitement"
new_word = word[6:]
The way of creating a substring above is similar to how we created a substring with only the first few letters before. By putting the code, ‘6:’, inside of the substring method, the new substring is loaded with the letters in our original String starting at index 6 and going until the end of the String.
In addition to these substring methods that can carry over to other programming languages, Python can also use index numbers that are not within the range of letters. For example, you can use negative numbers, which in Python don’t produce any index errors. When negative numbers are used, they count from the end of the String backwards.
word = "excitement"
new_word = word[-2:]
In the example shown above, the new substring that is formed will be “nt” since it is the last two letters of the original String.
Lastly, one more thing about using substrings in Python. The substring method has an additional 3rd parameter in Python. This 3rd parameter dictates the counting of the letters. So if the 3rd parameter is 1, every letter goes into the substring as if a 3rd parameter wasn’t there. If the 3rd parameter is 2, every other letter goes into the substring. If the 3rd parameter is 3, every 3rd letter goes into the substring.
word = "excitement"
word1 = word[0:6]
word2 = word[0:6:1]
word3 = word[0:6:2]
In the example shown above, word1 and word2 will be equivalent substrings. Both of these will be equal to “excite”. The substring that will differ will be the substring word3. The substring word3 will be equal to “ect”.