Strings
Learn all about the Python string object. Figure out how to automatically create, rearrange, reassign, disassemble, and reassemble blocks of text!
StartKey Concepts
Review core concepts you need to learn to master this subject
Strings
Escaping Characters
The in
Syntax
Indexing and Slicing Strings
Iterate String
Built-in Function len()
String Concatenation
Immutable strings
Strings
Strings
In computer science, sequences of characters are referred to as strings. Strings can be any length and can include any character such as letters, numbers, symbols, and whitespace (spaces, tabs, new lines).
Introduction to Strings
Lesson 1 of 3
- 1Words and sentences are fundamental to how we communicate, so it follows that we’d want our computers to be able to work with words and sentences as well. In Python, the way we store something li…
- 2A string can be thought of as a list of characters. Like any other list, each character in a string has an index. Consider the string: favorite_fruit = “blueberry” We can select specific le…
- 3Not only can we select a single character from a string, but we can also select entire chunks of characters from a string. We can do this with the following syntax: string[first_index:last_index] …
- 4We can also concatenate, or combine, two existing strings together into a new string. Consider the following two strings: fruit_prefix = “blue” fruit_suffix = “berries” We can create a new str…
- 5Python comes with some built-in functions for working with strings. One of the most commonly used of these functions is len(). len() returns the number of characters in a string: favorite_fruit = …
- 6In the previous exercise, we used len() to get a slice of characters at the end of a string. There’s a much easier way to do this — we can use negative indices! Negative indices count backwa…
- 7So far in this lesson, we’ve been selecting characters from strings, slicing strings, and concatenating strings. Each time we perform one of these operations we are creating an entirely new string….
- 8Occasionally when working with strings, you’ll find that you want to include characters that already have a special meaning in python. For example let’s say I create the string favorite_fruit_con…
- 9Now you know enough about strings that we can start doing the really fun stuff! Because strings are lists, that means we can iterate through a string using for or while loops. This opens up a whol…
- 10Now that we are iterating through strings, we can really explore the potential of strings. When we iterate through a string we do something with each character. By including conditional statement…
- 11There’s an even easier way than iterating through the entire string to determine if a character is in a string. We can do this type of check more efficiently using in. in checks if one string is pa…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory