Learn Python: Strings

Learn all about the Python string object. Figure out how to automatically create, rearrange, reassign, disassemble, and reassemble blocks of text!

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

String concatenation

x = 'One fish, ' y = 'two fish.' z = x + y print(z) # 'One fish, two fish.'

To combine the content of two strings into a single string, Python provides the + operator. This process of joining strings is called concatenation.

String lengths

x = 'One fish, ' y = 'two fish.' z = x + y print(z) # 'One fish, two fish.'

In Python, the built in function len() calculates the length of objects. It can be used to compute the length of strings, lists, sets, and other countable objects!

Chevron Left Icon
Introduction to Strings
Lesson 1 of 3
Chevron Right Icon
  1. 1

    Words 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...

  2. 2

    A 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 [...] We can select specific letters from this string ...

  3. 3

    Not only can we select a single character from a string, we can select entire chunks of characters from a string. We can do this with the following syntax: [...] This is called slicing a stri...

  4. 4

    You can also concatenate two existing strings together into a new string. Consider the following two strings. [...] We can create a new string by concatenating them together as follows: [...]...

  5. 5

    Python comes with some built-in functions for working with strings. One of the most commonly used of these functions is [...] . [...] returns the number of characters in a string [...] If yo...

  6. 6

    In the previous exercise, we used [...] 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 backward fr...

  7. 7

    So 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....

  8. 8

    Occasionally 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 [...] We'll have ...

  9. 9

    Now 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 [...] or [...] loops. This opens up ...

  10. 10

    Now 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...

  11. 11

    There'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 [...] . [...] checks if one st...

  12. 12

    Great work! I hope you are now starting to see the potential of strings and how they can be used to solve a huge variety of problems. In this lesson you learned: - A string is a list of characters...

  1. 1

    Do you have a gigantic string that you need to parse for information? Do you need to sanitize a users input to work in a function? Do you need to be able to generate outputs with variable values? A...

  2. 2

    There are three string methods that can change the casing of a string. These are [...] , [...] , and [...] . - [...] returns the string with all lowercase characters. - [...] returns the st...

  3. 3

    [...] , [...] , and [...] all are performed on an existing string and produce a string in return. Let's take a look at a string method that returns a different object entirely! [...] is perf...

  4. 4

    If we provide an argument for [...] we can dictate the character we want our string to be split on. This argument should be provided as a string itself. Consider the following example: [...] ...

  5. 5

    We can also split strings using escape sequences. Escape sequences are used to indicate that we want to split by something in a string that is not necessarily a character. The two escape sequen...

  6. 6

    Now that you've learned to break strings apart using [...] , let's learn to put them back together using [...] . [...] is essentially the opposite of [...] , it joins a list of strings toget...

  7. 7

    In the last exercise, we joined together a list of words using a space as the delimiter to create a sentence. In fact, you can use any string as a delimiter to join together a list of strings. For ...

  8. 8

    When working with strings that come from real data, you will often find that the strings aren't super clean. You'll find lots of extra whitespace, unnecessary linebreaks, and rogue tabs. Python p...

  9. 9

    The next string method we will cover is [...] . Replace takes two arguments and replaces all instances of the first argument in a string with the second argument. The syntax is as follows [...]...

  10. 10

    Another interesting string method is [...] . [...] takes a string as an argument and searching the string it was run on for that string. It then returns the first index value where that string...

  11. 11

    Python also provides a handy string method for including variables in strings. This method is [...] . [...] takes variables as an argument and includes them in the string that it is run on. You ...

  12. 12

    [...] can be made even more legible for other people reading your code by including keywords. Previously with [...] , you had to make sure that your variables appeared as arguments in the same...

  13. 13

    Excellent work! This lesson has shown you the vast variety of string methods and their power. Whatever the problem you are trying to solve, if you are working with strings then string methods are l...

  1. 1

    This lesson will help you review Python functions by providing some challenge exercises involving Strings. As a refresher, function syntax looks like this: [...] For example, a function that f...

  2. 2

    [...]

  3. 3

    [...]

  4. 4

    [...]

  5. 5

    [...]

  6. 6

    [...]

  7. 7

    [...]

  8. 8

    [...]

  9. 9

    [...]

  10. 10

    [...]

  11. 11

    [...]

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Learn Python: Strings

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]