Introduction

In the previous guide, you learned the basics of handling strings in Python. In this guide, you will explore some of the more complex things that strings are capable of. There are tools built into Python, called string methods, that help you to handle strings and do some very powerful things. Through the use of string methods, you can masterfully manipulate text and use it to its fullest potential without writing a ton of code.

Navigating a String

Strings aren't words. They aren't sentences, phrases, and believe it or not, they aren't even a collection of text. Strings are just a lists of characters. Those characters can be letter, numbers, symbols, spaces, and escape characters. Python sees strings by their parts(the characters) and uses those parts to manipulate strings. This is actually true of almost any programming language. So, that means that you can select individual characters out of a string. Try this:
phrase_string = "This phrase is a string!"
print(phrase_string[0])
The letter T was printed out. If you've never programmed before, you have to know that computers are weird. They start counting at zero. So, whenever you are dealing with a situation where a computer has to count through something, it will start at zero. The zero, or first, place in phrase_string is the letter T.

Try this next one:
phrase_string = "This phrase is a string!"
print(phrase_string[4])
Did it do anything? Actually, it did. Notice what is in the fifth(4) place in the string. It's the space. Python is outputting that, since spaces are characters.

You can also count backward. It might not seem that reasonable at first, but some strings are very long. It can be helpful be able to access the opposite end of a string easily. Counting from the opposite end of the string still assumes that the string starts at zero, but goes backward into the negatives. So, to quickly access the last character of a string, select negative one.
phrase_string = "This phrase is a string!"
print(phrase_string[-1])
Depending on the position of the character that you need, you can access it by either its positive or negative value, making accessing any character in a string simple and efficient.

The Length of Strings

Instances will arise when you need to know how long a string is. Thankfully, Python has made finding out especially easy with the len() method. All that you have to do is place the string or variable representing the string in the parenthesis.
phrase_string = "This phrase is a string!"
print(len(phrase_string))
You should see an output of 24. Keep in mind that the last character of the string is accessible at 23 or -1, since Python starts counting at zero.

Slicing Strings

You can select more than just one character from a string. You can slice out sections of a string using the positions of the first and last characters of the area that you'd like to select. To get just the word "phrase" in "This phrase is a string," you could do the following:
phrase_string = "This phrase is a string!"
print(phrase_string[5:11])
That statement is saying to start slicing the string before the fifth character and stop before the eleventh. To think of it another way; you are telling it to start on the fifth character and stop before the eleventh. The starting point is inclusive and the ending is exclusive.

In order to start at the beginning of the string, just leave off the first number.
phrase_string = "This phrase is a string!"
print(phrase_string[:4])
If you want to continue to the end, leave off the last number.
phrase_string = "This phrase is a string!"
print(phrase_string[17:])
This also works backward.
phrase_string = "This phrase is a string!"
print(phrase_string[-7:])
The above example results in the same thing as the example before, string!.

Upper and Lower Case

Python makes capitalizing or lower casing strings very easy. There are two built in methods, upper() and lower() that will either capitalize or lowercase the entire string. Give them a try.
phrase_string = "This phrase is a string!"
print(phrase_string.upper())
print(phrase_string.lower())
Notice that these methods are different than len(). They are tacked on at the end with a .. This means that they are built into the strings themselves, and aren't coming from elsewhere in Python. That's an important distinction to make later on, and it applies to more than just strings.

Find and Replace

Find and find and replace are very useful tools in word processing programs. Python makes their functionality available in dealing with strings. They aren't exactly the same, though, so it's best to try them out and get a feel for how they work.

The find() method actually just finds the first occurrence of the set of characters in the parenthesis and tells you the character where it begins.
phrase_string = "This phrase is a string!"
print(phrase_string.find("is"))
Do you see what happened? It returned 2. That's the position of the first time the pattern "is" appears. Python wasn't looking for the word, "is." It was looking for the patter of the letter "i" followed by the letter "s." It's important to be careful when searching through strings when using find().

replace() finds every occurrence of a set of characters and replaces them with another set. This works the same way that find() does and finds a pattern, regardless of where it is in a word. The way around this is to add the space characters into the set that you're finding and replacing them in the changed text.
phrase_string = "This phrase is a string!"
print(phrase_string.replace(" is ", " was "))

Conclusion

There are more string methods available, and you can look them up as you need them. The goal of this guide and the preceding one is so provide you with a strong basis for working with strings in Python. Now, you should be able to proceed confidently, knowing that you can handle text when you need to.

Table of Contents

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions